From e1a80298c323e0de6aef693e4324daff792ba9b6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Apr 2022 09:28:16 +0000 Subject: [PATCH 001/138] Auto-generated commit cff4f263cdf4c918b04d3eabf25e55a7e7a051ff --- CONTRIBUTORS | 28 + LICENSE | 481 +++++++++ NOTICE | 1 + README.md | 520 ++++++++++ index.d.ts | 411 ++++++++ index.mjs | 4 + index.mjs.map | 1 + package.json | 43 + stats.html | 2689 +++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 4178 insertions(+) create mode 100644 CONTRIBUTORS create mode 100644 LICENSE create mode 100644 NOTICE create mode 100644 README.md create mode 100644 index.d.ts create mode 100644 index.mjs create mode 100644 index.mjs.map create mode 100644 package.json create mode 100644 stats.html diff --git a/CONTRIBUTORS b/CONTRIBUTORS new file mode 100644 index 0000000..e5c896d --- /dev/null +++ b/CONTRIBUTORS @@ -0,0 +1,28 @@ +# This file is generated by tools/scripts/update_contributors. +# +# Contributors listed in alphabetical order. + +Ali Salesi +Athan Reines +Brendan Graetz +Bruno Fenzl +Christopher Dambamuromo +Dominik Moritz +Frank Kovacs +James +Jithin KS +Joey Reed +Joris Labie +Justin Dennison +Marcus +Matt Cochrane +Milan Raj +Momtchil Momtchev +Ognjen Jevremović +Philipp Burckhardt +Ricky Reusser +Ryan Seal +Seyyed Parsa Neshaei +Shraddheya Shendre +dorrin-sot <59933477+dorrin-sot@users.noreply.github.com> +rei2hu diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fcc9934 --- /dev/null +++ b/LICENSE @@ -0,0 +1,481 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by this +license (the "Software") to use, reproduce, display, distribute, execute, and +transmit the Software, and to prepare derivative works of the Software, and to +permit third-parties to whom the Software is furnished to do so, all subject to +the following: + +The copyright notices in the Software and this entire statement, including the +above license grant, this restriction and the following disclaimer, must be +included in all copies of the Software, in whole or in part, and all derivative +works of the Software, unless such copies or derivative works are solely in the +form of machine-executable object code generated by a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES +OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + + + +DEPENDENCIES + +The library links against the following external libraries, which have their own +licenses: + +* OpenBLAS + +Copyright (c) 2011-2014, The OpenBLAS Project +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + 3. Neither the name of the OpenBLAS project nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* Electron + +Copyright (c) 2013-2017 GitHub Inc. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +* Boost + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + + +* Cephes + +Copyright (c) 1984-2000 Stephen L. Moshier + +Some software in this archive may be from the book _Methods and Programs for +Mathematical Functions_ (Prentice-Hall or Simon & Schuster International, 1989) +or from the Cephes Mathematical Library, a commercial product. In either event, +it is copyrighted by the author. What you see here may be used freely but it +comes with no support or guarantee. + +Stephen L. Moshier +moshier@na-net.ornl.gov + + + +ATTRIBUTION + +The library contains implementations from the following external libraries, +which have their own licenses: + +* FreeBSD + +Copyright (C) 1993-2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + + +* FDLIBM + +Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + + +* Go + +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* SLATEC Common Mathematical Library + +Public domain. + + +* ESLint + +Copyright JS Foundation and other contributors, https://js.foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + +* StatsFuns.jl + +Copyright (c) 2015: Dahua Lin. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +* SpecialFunctions.jl + +The MIT License (MIT) + +Copyright (c) 2017 Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and others: + +https://github.com/JuliaMath/SpecialFunctions.jl/graphs/contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +* MT19937 + +Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..10e6ba1 --- /dev/null +++ b/NOTICE @@ -0,0 +1 @@ +Copyright (c) 2016-2022 The Stdlib Authors. diff --git a/README.md b/README.md new file mode 100644 index 0000000..03a5fa1 --- /dev/null +++ b/README.md @@ -0,0 +1,520 @@ + + +# reduceAsync + +[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] + +> Apply a function against an accumulator and each element in a collection and return the accumulated result. + + + +
+ +
+ + + + + + + +
+ +## Usage + +```javascript +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +``` + +#### reduceAsync( collection, initial, \[options,] reducer, done ) + +Applies a `function` against an accumulator and each element in a `collection` and returns the accumulated result. + +```javascript +function reducer( acc, value, index, next ) { + setTimeout( onTimeout, value ); + function onTimeout() { + console.log( value ); + acc.sum += value; + next( null, acc ); + } +} + +function done( error, acc ) { + if ( error ) { + throw error; + } + console.log( acc.sum ); +} + +var arr = [ 3000, 2500, 1000 ]; +var acc = { + 'sum': 0 +}; +reduceAsync( arr, acc, reducer, done ); +/* + 3000 + 2500 + 1000 + 6500 +*/ +``` + +The `next` callback accepts two arguments: `error` and `accumulator`. The second argument to the `next` callback is passed as the first argument to the provided `reducer`. + + + +```javascript +function reducer( acc, value, index, next ) { + setTimeout( onTimeout, value ); + function onTimeout() { + next( null, acc ); + } +} + +function done( error, out ) { + if ( error ) { + throw error; + } + console.log( out === acc ); + // => true +} + +var arr = [ 3000, 2500, 1000 ]; +var acc = {}; + +reduceAsync( arr, acc, reducer, done ); +``` + +The function accepts the following `options`: + +- `limit`: the maximum number of pending invocations at any one time. If provided, the function sets `options.series=false`. Default: `infinity`. +- `series`: `boolean` indicating whether to sequentially invoke `reducer` for each `collection` element. If `true`, the function sets `options.limit=1`. Default: `true`. +- `thisArg`: the execution context for `reducer`. + +By default, all elements are processed **sequentially**, which means that the function **does** guarantee completion order. To process each `collection` element concurrently, set the `series` option to `false`. + +```javascript +function reducer( acc, value, index, next ) { + setTimeout( onTimeout, value ); + function onTimeout() { + console.log( value ); + acc.sum += value; + next( null, acc ); + } +} + +function done( error, acc ) { + if ( error ) { + throw error; + } + console.log( acc.sum ); +} + +var arr = [ 3000, 2500, 1000 ]; + +var acc = { + 'sum': 0 +}; + +var opts = { + 'series': false +}; + +reduceAsync( arr, acc, opts, reducer, done ); +/* => + 1000 + 2500 + 3000 + 6500 +*/ +``` + +To limit the maximum number of pending function invocations, set the `limit` option. + +```javascript +function reducer( acc, value, index, next ) { + setTimeout( onTimeout, value ); + function onTimeout() { + console.log( value ); + acc.sum += value; + next( null, acc ); + } +} + +function done( error, acc ) { + if ( error ) { + throw error; + } + console.log( acc.sum ); +} + +var arr = [ 3000, 2500, 1000 ]; + +var acc = { + 'sum': 0 +}; + +var opts = { + 'limit': 2 +}; + +reduceAsync( arr, acc, opts, reducer, done ); +/* => + 2500 + 3000 + 1000 + 6500 +*/ +``` + +To set the execution context of `reducer`, set the `thisArg` option. + +```javascript +function reducer( acc, value, index, next ) { + this.count += 1; + setTimeout( onTimeout, value ); + function onTimeout() { + acc.sum += value; + next( null, acc ); + } +} + +var arr = [ 3000, 2500, 1000 ]; + +var acc = { + 'sum': 0 +}; + +var context = { + 'count': 0 +}; + +var opts = { + 'thisArg': context +}; + +reduceAsync( arr, acc, opts, reducer, done ); + +function done( error, acc ) { + if ( error ) { + throw error; + } + console.log( acc.sum ); + // => 6500 + + console.log( context.count ); + // => 3 +} +``` + +When invoked, `reducer` is provided a maximum of five arguments: + +- `accumulator`: accumulated value. +- `value`: collection value. +- `index`: collection index. +- `collection`: the input `collection`. +- `next`: a callback which should be called once `reducer` has finished processing a collection `value`. + +The actual number of provided arguments depends on function `length`. If `reducer` accepts three arguments, `reducer` is provided `accumulator`, `value` and `next`. If `reducer` accepts four arguments, `reducer` is provided `accumulator`, `value`, `index`, and `next`. For every other `reducer` signature, `reducer` is provided all five arguments. + +```javascript +function reducer( acc, value, i, collection, next ) { + console.log( 'collection: %s. %d: %d', collection.join( ',' ), i, value ); + setTimeout( onTimeout, value ); + function onTimeout() { + console.log( value ); + acc.sum += value; + next( null, acc ); + } +} + +function done( error, acc ) { + if ( error ) { + throw error; + } + console.log( acc.sum ); +} + +var arr = [ 3000, 2500, 1000 ]; + +var acc = { + 'sum': 0 +}; + +reduceAsync( arr, acc, reducer, done ); +/* => + collection: 3000,2500,1000. 0: 3000 + collection: 3000,2500,1000. 1: 2500 + collection: 3000,2500,1000. 2: 1000 + 3000 + 2500 + 1000 + 6500 +*/ +``` + +#### reduceAsync.factory( \[options,] reducer ) + +Returns a `function` which invokes a function once for each element in a `collection`. + +```javascript +function reducer( acc, value, index, next ) { + setTimeout( onTimeout, value ); + function onTimeout() { + console.log( value ); + acc.sum += value; + next( null, acc ); + } +} + +function done( error, acc ) { + if ( error ) { + throw error; + } + console.log( acc.sum ); +} + +var f = reduceAsync.factory( reducer ); + +var arr1 = [ 3000, 2500, 1000 ]; + +var acc1 = { + 'sum': 0 +}; + +f( arr1, acc1, done ); +/* => + 3000 + 2500 + 1000 + 6500 +*/ + +var arr2 = [ 300, 250, 100 ]; + +var acc2 = { + 'sum': 0 +}; + +f( arr2, acc2, done ); +/* => + 300 + 250 + 100 + 650 +*/ +``` + +The function accepts the same `options` as `reduceAsync()`. + +
+ + + + + +
+ +## Notes + +- A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). +- If a provided function calls the `next` callback with a truthy `error` argument, the function suspends execution and immediately calls the `done` callback for subsequent `error` handling. +- The function invokes the `done` callback with the `accumulator` provided as the second argument. If provided an empty `collection`, the function invokes the `done` callback with the `initial` value as the second argument. +- The function does **not** support dynamic `collection` resizing. +- The function does **not** skip `undefined` elements. +- When processing `collection` elements concurrently, **beware** of race conditions when updating an accumulator. This is especially true when an accumulator is a primitive (e.g., a `number`). In general, prefer `object` accumulators, as objects are passed by reference, not by value. +- **Neither** `reduceAsync` nor the function returned by the `factory` method **guarantee** asynchronous execution. To guarantee asynchrony, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). + +
+ + + + + +
+ +## Examples + + + +```html + + + + + + +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + +
+ +* * * + +## Notice + +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. + +For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. + +#### Community + +[![Chat][chat-image]][chat-url] + +--- + +## License + +See [LICENSE][stdlib-license]. + + +## Copyright + +Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. + +
+ + + + + + + + diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..d1fd90f --- /dev/null +++ b/index.d.ts @@ -0,0 +1,411 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/// + +import { Collection } from '@stdlib/types/object'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * The maximum number of pending invocations at any one time. + */ + limit?: number; + + /** + * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). + */ + series?: boolean; + + /** + * Execution context. + */ + thisArg?: any; +} + +/** +* Callback function. +*/ +type Nullary = () => void; + +/** +* Callback function. +* +* @param error - encountered error or null +*/ +type Unary = ( error: Error | null ) => void; + +/** +* Callback function. +* +* @param error - encountered error or null +* @param accumulator - accumulated value +*/ +type Binary = ( error: Error | null, accumulator: any ) => void; + +/** +* Callback function. +* +* @param error - encountered error or null +* @param accumulator - accumulated value +*/ +type Callback = Nullary | Unary | Binary; + +/** +* Reducer function invoked for each element in a collection. +* +* @param accumulator - accumulated value +* @param value - collection value +* @param next - a callback to be invoked after processing a collection `value` +*/ +type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; + +/** +* Reducer function invoked for each element in a collection. +* +* @param accumulator - accumulated value +* @param value - collection value +* @param index - collection index +* @param next - a callback to be invoked after processing a collection `value` +*/ +type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length + +/** +* Reducer function invoked for each element in a collection. +* +* @param accumulator - accumulated value +* @param value - collection value +* @param index - collection index +* @param collection - the input collection +* @param next - a callback to be invoked after processing a collection `value` +*/ +type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length + +/** +* Reducer function invoked for each element in a collection. +* +* @param accumulator - accumulated value +* @param value - collection value +* @param index - collection index +* @param collection - the input collection +* @param next - a callback to be invoked after processing a collection `value` +*/ +type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; + +/** +* Applies a function against an accumulator and each element in a collection and return the accumulated result. +* +* @param collection - input collection +* @param initial - initial value +* @param done - function to invoke upon completion +*/ +type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length + +/** +* Interface for `reduceAsync`. +*/ +interface ReduceAsync { + /** + * Applies a function against an accumulator and each element in a collection and return the accumulated result. + * + * ## Notes + * + * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. + * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). + * + * + * @param collection - input collection + * @param initial - initial value + * @param options - function options + * @param options.thisArg - execution context + * @param options.limit - maximum number of pending invocations at any one time + * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) + * @param reducer - function to invoke for each element in a collection + * @param done - function to invoke upon completion + * @throws must provide valid options + * + * @example + * var readFile = require( `@stdlib/fs/read-file` ); + * + * function done( error, acc ) { + * if ( error ) { + * throw error; + * } + * console.log( acc.count ); + * } + * + * function read( acc, file, next ) { + * var opts = { + * 'encoding': 'utf8' + * }; + * readFile( file, opts, onFile ); + * + * function onFile( error ) { + * if ( error ) { + * return next( null, acc ); + * } + * acc.count += 1; + * next( null, acc ); + * } + * } + * + * var files = [ + * './beep.js', + * './boop.js' + * ]; + * var acc = { + * 'count': 0 + * }; + * reduceAsync( files, acc, {}, read, done ); + */ + ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length + + /** + * Applies a function against an accumulator and each element in a collection and return the accumulated result. + * + * ## Notes + * + * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. + * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). + * + * + * @param collection - input collection + * @param initial - initial value + * @param reducer - function to invoke for each element in a collection + * @param done - function to invoke upon completion + * + * @example + * var readFile = require( `@stdlib/fs/read-file` ); + * + * function done( error, acc ) { + * if ( error ) { + * throw error; + * } + * console.log( acc.count ); + * } + * + * function read( acc, file, next ) { + * var opts = { + * 'encoding': 'utf8' + * }; + * readFile( file, opts, onFile ); + * + * function onFile( error ) { + * if ( error ) { + * return next( null, acc ); + * } + * acc.count += 1; + * next( null, acc ); + * } + * } + * + * var files = [ + * './beep.js', + * './boop.js' + * ]; + * var acc = { + * 'count': 0 + * }; + * reduceAsync( files, acc, read, done ); + */ + ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length + + /** + * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. + * + * ## Notes + * + * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. + * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). + * + * + * @param options - function options + * @param options.thisArg - execution context + * @param options.limit - maximum number of pending invocations at any one time + * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) + * @param reducer - function to invoke for each element in a collection + * @throws must provide valid options + * @returns function which invokes the provided function once for each element in a collection + * + * @example + * var readFile = require( `@stdlib/fs/read-file` ); + * + * function read( acc, file, next ) { + * var opts = { + * 'encoding': 'utf8' + * }; + * readFile( file, opts, onFile ); + * + * function onFile( error, data ) { + * if ( error ) { + * return next( null, acc ); + * } + * acc.count += 1; + * next( null, acc ); + * } + * } + * + * var opts = { + * 'series': false + * }; + * + * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: + * var reduceAsync = factory( opts, read ); + * + * // Create a collection over which to iterate: + * var files = [ + * './beep.js', + * './boop.js' + * ]; + * + * // Define a callback which handles errors: + * function done( error, acc ) { + * if ( error ) { + * throw error; + * } + * console.log( acc.count ); + * } + * + * // Run `read` for each element in `files`: + * var acc = { + * 'count': 0 + * }; + * reduceAsync( files, acc, done ); + */ + factory( options: Options, reducer: Reducer ): FactoryFunction; + + /** + * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. + * + * ## Notes + * + * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. + * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). + * + * + * @param reducer - function to invoke for each element in a collection + * @throws must provide valid options + * @returns function which invokes the provided function once for each element in a collection + * + * @example + * var readFile = require( `@stdlib/fs/read-file` ); + * + * function read( acc, file, next ) { + * var opts = { + * 'encoding': 'utf8' + * }; + * readFile( file, opts, onFile ); + * + * function onFile( error, data ) { + * if ( error ) { + * return next( null, acc ); + * } + * acc.count += 1; + * next( null, acc ); + * } + * } + * + * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: + * var reduceAsync = factory( read ); + * + * // Create a collection over which to iterate: + * var files = [ + * './beep.js', + * './boop.js' + * ]; + * + * // Define a callback which handles errors: + * function done( error, acc ) { + * if ( error ) { + * throw error; + * } + * console.log( acc.count ); + * } + * + * // Run `read` for each element in `files`: + * var acc = { + * 'count': 0 + * }; + * reduceAsync( files, acc, done ); + */ + factory( reducer: Reducer ): FactoryFunction; +} + +/** +* Applies a function against an accumulator and each element in a collection and return the accumulated result. +* +* ## Notes +* +* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. +* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). +* +* +* @param collection - input collection +* @param initial - initial value +* @param options - function options +* @param options.thisArg - execution context +* @param options.limit - maximum number of pending invocations at any one time +* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) +* @param reducer - function to invoke for each element in a collection +* @param done - function to invoke upon completion +* @throws must provide valid options +* +* @example +* var readFile = require( `@stdlib/fs/read-file` ); +* +* function done( error, acc ) { +* if ( error ) { +* throw error; +* } +* console.log( acc.count ); +* } +* +* function read( acc, file, next ) { +* var opts = { +* 'encoding': 'utf8' +* }; +* readFile( file, opts, onFile ); +* +* function onFile( error ) { +* if ( error ) { +* return next( null, acc ); +* } +* acc.count += 1; +* next( null, acc ); +* } +* } +* +* var files = [ +* './beep.js', +* './boop.js' +* ]; +* var acc = { +* 'count': 0 +* }; +* reduceAsync( files, acc, read, done ); +*/ +declare var reduceAsync: ReduceAsync; + + +// EXPORTS // + +export = reduceAsync; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..172f6bc --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import l from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import u from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function a(e){if(e.__esModule)return e;var t=Object.defineProperty({},"__esModule",{value:!0});return Object.keys(e).forEach((function(r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})})),t}var c=o,m=s,f=l.isPrimitive,d=u.isPrimitive,h=n;var p=function(e,t){return c(t)?(m(t,"thisArg")&&(e.thisArg=t.thisArg),m(t,"series")&&(e.series=t.series,!f(e.series))?new TypeError(h("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):m(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(h("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(h("invalid argument. Options argument must be an object. Value: `%s`.",t))},g="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function v(){throw new Error("setTimeout has not been defined")}function w(){throw new Error("clearTimeout has not been defined")}var b=v,j=w;function y(e){if(b===setTimeout)return setTimeout(e,0);if((b===v||!b)&&setTimeout)return b=setTimeout,setTimeout(e,0);try{return b(e,0)}catch(t){try{return b.call(null,e,0)}catch(t){return b.call(this,e,0)}}}"function"==typeof g.setTimeout&&(b=setTimeout),"function"==typeof g.clearTimeout&&(j=clearTimeout);var T,x=[],E=!1,O=-1;function A(){E&&T&&(E=!1,T.length?x=T.concat(x):O=-1,x.length&&_())}function _(){if(!E){var e=y(A);E=!0;for(var t=x.length;t;){for(T=x,x=[];++O1)for(var r=1;r1){if(n=Q(r,e))throw n;i=t}else i=e;if(!G(i))throw new TypeError(I("invalid argument. Last argument must be a function. Value: `%s`.",i));return void 0===r.series&&void 0===r.limit&&(r.series=!0),r.series?r.limit=1:r.limit||(r.limit=K),o;function o(e,t,n){if(!H(e))throw new TypeError(I("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!G(n))throw new TypeError(I("invalid argument. Last argument must be a function. Value: `%s`.",n));return R(e,t,r,i,(function(e,t){if(e)return n(e);n(null,t)}))}},W=U;var X=function(e,t,r,n,i){if(arguments.length<5)return W(r)(e,t,n);W(r,n)(e,t,i)},Y=U;e(X,"factory",Y);var Z=X;export{Z as default,Y as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..e7ba9bb --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../node_modules/debug/src/index.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n","/**\n * Detect Electron renderer process, which is node, but we should\n * treat as a browser.\n */\n\nif (typeof process !== 'undefined' && process.type === 'renderer') {\n module.exports = require('./browser.js');\n} else {\n module.exports = require('./node.js');\n}\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar reduceAsync = require( './reduce.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n"],"names":["isObject","require$$0","hasOwnProp","require$$1","isBoolean","require$$2","isPrimitive","isPositiveInteger","require$$3","format","require$$4","validate_1","opts","options","thisArg","series","TypeError","limit","module","exports","process","type","require","debug","logger","isFunction","isCollection","PINF","validate","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory_1","err","f","arguments","reduceAsync","initial","factory","lib"],"mappings":";;2jCAsBA,IAAIA,EAAWC,EACXC,EAAaC,EACbC,EAAYC,EAAuCC,YACnDC,EAAoBC,EAAgDF,YACpEG,EAASC,EAqDb,IAAAC,EAzBA,SAAmBC,EAAMC,GACxB,OAAMb,EAAUa,IAGXX,EAAYW,EAAS,aACzBD,EAAKE,QAAUD,EAAQC,SAEnBZ,EAAYW,EAAS,YACzBD,EAAKG,OAASF,EAAQE,QAChBX,EAAWQ,EAAKG,SACd,IAAIC,UAAWP,EAAQ,+DAAgE,SAAUG,EAAKG,SAG1Gb,EAAYW,EAAS,WACzBD,EAAKK,MAAQJ,EAAQI,OACfV,EAAmBK,EAAKK,QACtB,IAAID,UAAWP,EAAQ,wEAAyE,QAASG,EAAKK,QAGhH,MAjBC,IAAID,UAAWP,EAAQ,qEAAsEI,27DClDpGK,OAAOC,aADc,IAAZC,GAA4C,aAAjBA,EAAQC,KAC3BC,QAAQ,gBAERA,QAAQ,iBCmBvBC,oCAAQC,CAAQ,sBAoHpB,ICzHIC,EAAaxB,EACbyB,EAAevB,EACfM,EAASJ,EACTsB,EAAOnB,EACPoB,EAAWlB,EACXO,EDkBJ,SAAgBY,EAAYC,EAAKlB,EAAMmB,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBjB,EAAO,wBAAyBc,GAEnB,IAARA,EAEJ,OADAd,EAAO,qCACAS,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMzB,EAAKK,MACToB,EAEAzB,EAAKK,MAEZM,EAAO,wBAAyBa,GAChCb,EAAO,0BAA2BQ,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdpB,EAAO,kBAAmBuB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDtB,EAAO,6BADPe,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMpC,EAAKE,QAASgB,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMpC,EAAKE,QAASgB,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMpC,EAAKE,QAASgB,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJpB,EAAO,2BAA4BoB,EAAMM,SAClCjB,EAAMW,KAGdpB,EAAO,0CADPW,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdd,EAAO,qCACAS,EAAM,KAAMF,SAFpB,KC+BF,IAAAoB,EApEA,SAAkBrC,EAASkB,GAC1B,IAAInB,EACAuC,EACAC,EAGJ,GADAxC,EAAO,GACFyC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMvB,EAAUhB,EAAMC,GAErB,MAAMsC,EAEPC,EAAIrB,OAEJqB,EAAIvC,EAEL,IAAMY,EAAY2B,GACjB,MAAM,IAAIpC,UAAWP,EAAQ,mEAAoE2C,IAUlG,YARqB,IAAhBxC,EAAKG,aAAoC,IAAfH,EAAKK,QACnCL,EAAKG,QAAS,GAEVH,EAAKG,OACTH,EAAKK,MAAQ,EACDL,EAAKK,QACjBL,EAAKK,MAAQU,GAEP2B,EAaP,SAASA,EAAazB,EAAY0B,EAASvB,GAC1C,IAAMN,EAAcG,GACnB,MAAM,IAAIb,UAAWP,EAAQ,sEAAuEoB,IAErG,IAAMJ,EAAYO,GACjB,MAAM,IAAIhB,UAAWP,EAAQ,mEAAoEuB,IAElG,OAAOf,EAAOY,EAAY0B,EAAS3C,EAAMwC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,QCtIX0B,EAAUvD,EAyEd,IC9BAqD,EDoBA,SAAsBzB,EAAY0B,EAAS1C,EAASkB,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOgB,EAAS3C,EAAT2C,CAAoB3B,EAAY0B,EAASxB,GAEjDyB,EAAS3C,EAASkB,EAAlByB,CAAyB3B,EAAY0B,EAASvB,ICvB/CwB,EAAAnD,EAFAJ,EAOAqD,EAAA,UAAAE,GAKA,IAAAC,EAAAH"} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..b4af9bd --- /dev/null +++ b/package.json @@ -0,0 +1,43 @@ +{ + "name": "@stdlib/utils-async-reduce", + "version": "0.0.0", + "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", + "license": "Apache-2.0", + "type": "module", + "main": "./index.mjs", + "homepage": "https://stdlib.io", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/utils-async-reduce.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "keywords": [ + "stdlib", + "stdutils", + "stdutil", + "utilities", + "utility", + "utils", + "util", + "asynchronous", + "async", + "reduce", + "reducer", + "reduction", + "array.reduce", + "iterate", + "aggregate", + "accumulator", + "accumulate", + "accumulation", + "collection", + "array", + "array-like" + ], + "funding": { + "type": "patreon", + "url": "https://www.patreon.com/athan" + } +} diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..99cb65c --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + From 40952dd81ae232930dd739645e8c36a59ae29f89 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Jul 2022 07:29:14 +0000 Subject: [PATCH 002/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From c609d2432995aa9c184b685aaf3a2fa9f0c7364e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Jul 2022 18:40:17 +0000 Subject: [PATCH 003/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 172f6bc..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import l from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import u from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function a(e){if(e.__esModule)return e;var t=Object.defineProperty({},"__esModule",{value:!0});return Object.keys(e).forEach((function(r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})})),t}var c=o,m=s,f=l.isPrimitive,d=u.isPrimitive,h=n;var p=function(e,t){return c(t)?(m(t,"thisArg")&&(e.thisArg=t.thisArg),m(t,"series")&&(e.series=t.series,!f(e.series))?new TypeError(h("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):m(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(h("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(h("invalid argument. Options argument must be an object. Value: `%s`.",t))},g="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function v(){throw new Error("setTimeout has not been defined")}function w(){throw new Error("clearTimeout has not been defined")}var b=v,j=w;function y(e){if(b===setTimeout)return setTimeout(e,0);if((b===v||!b)&&setTimeout)return b=setTimeout,setTimeout(e,0);try{return b(e,0)}catch(t){try{return b.call(null,e,0)}catch(t){return b.call(this,e,0)}}}"function"==typeof g.setTimeout&&(b=setTimeout),"function"==typeof g.clearTimeout&&(j=clearTimeout);var T,x=[],E=!1,O=-1;function A(){E&&T&&(E=!1,T.length?x=T.concat(x):O=-1,x.length&&_())}function _(){if(!E){var e=y(A);E=!0;for(var t=x.length;t;){for(T=x,x=[];++O1)for(var r=1;r1){if(n=Q(r,e))throw n;i=t}else i=e;if(!G(i))throw new TypeError(I("invalid argument. Last argument must be a function. Value: `%s`.",i));return void 0===r.series&&void 0===r.limit&&(r.series=!0),r.series?r.limit=1:r.limit||(r.limit=K),o;function o(e,t,n){if(!H(e))throw new TypeError(I("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!G(n))throw new TypeError(I("invalid argument. Last argument must be a function. Value: `%s`.",n));return R(e,t,r,i,(function(e,t){if(e)return n(e);n(null,t)}))}},W=U;var X=function(e,t,r,n,i){if(arguments.length<5)return W(r)(e,t,n);W(r,n)(e,t,i)},Y=U;e(X,"factory",Y);var Z=X;export{Z as default,Y as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index e7ba9bb..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../node_modules/debug/src/index.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n","/**\n * Detect Electron renderer process, which is node, but we should\n * treat as a browser.\n */\n\nif (typeof process !== 'undefined' && process.type === 'renderer') {\n module.exports = require('./browser.js');\n} else {\n module.exports = require('./node.js');\n}\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar reduceAsync = require( './reduce.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n"],"names":["isObject","require$$0","hasOwnProp","require$$1","isBoolean","require$$2","isPrimitive","isPositiveInteger","require$$3","format","require$$4","validate_1","opts","options","thisArg","series","TypeError","limit","module","exports","process","type","require","debug","logger","isFunction","isCollection","PINF","validate","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory_1","err","f","arguments","reduceAsync","initial","factory","lib"],"mappings":";;2jCAsBA,IAAIA,EAAWC,EACXC,EAAaC,EACbC,EAAYC,EAAuCC,YACnDC,EAAoBC,EAAgDF,YACpEG,EAASC,EAqDb,IAAAC,EAzBA,SAAmBC,EAAMC,GACxB,OAAMb,EAAUa,IAGXX,EAAYW,EAAS,aACzBD,EAAKE,QAAUD,EAAQC,SAEnBZ,EAAYW,EAAS,YACzBD,EAAKG,OAASF,EAAQE,QAChBX,EAAWQ,EAAKG,SACd,IAAIC,UAAWP,EAAQ,+DAAgE,SAAUG,EAAKG,SAG1Gb,EAAYW,EAAS,WACzBD,EAAKK,MAAQJ,EAAQI,OACfV,EAAmBK,EAAKK,QACtB,IAAID,UAAWP,EAAQ,wEAAyE,QAASG,EAAKK,QAGhH,MAjBC,IAAID,UAAWP,EAAQ,qEAAsEI,27DClDpGK,OAAOC,aADc,IAAZC,GAA4C,aAAjBA,EAAQC,KAC3BC,QAAQ,gBAERA,QAAQ,iBCmBvBC,oCAAQC,CAAQ,sBAoHpB,ICzHIC,EAAaxB,EACbyB,EAAevB,EACfM,EAASJ,EACTsB,EAAOnB,EACPoB,EAAWlB,EACXO,EDkBJ,SAAgBY,EAAYC,EAAKlB,EAAMmB,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBjB,EAAO,wBAAyBc,GAEnB,IAARA,EAEJ,OADAd,EAAO,qCACAS,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMzB,EAAKK,MACToB,EAEAzB,EAAKK,MAEZM,EAAO,wBAAyBa,GAChCb,EAAO,0BAA2BQ,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdpB,EAAO,kBAAmBuB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDtB,EAAO,6BADPe,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMpC,EAAKE,QAASgB,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMpC,EAAKE,QAASgB,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMpC,EAAKE,QAASgB,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJpB,EAAO,2BAA4BoB,EAAMM,SAClCjB,EAAMW,KAGdpB,EAAO,0CADPW,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdd,EAAO,qCACAS,EAAM,KAAMF,SAFpB,KC+BF,IAAAoB,EApEA,SAAkBrC,EAASkB,GAC1B,IAAInB,EACAuC,EACAC,EAGJ,GADAxC,EAAO,GACFyC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMvB,EAAUhB,EAAMC,GAErB,MAAMsC,EAEPC,EAAIrB,OAEJqB,EAAIvC,EAEL,IAAMY,EAAY2B,GACjB,MAAM,IAAIpC,UAAWP,EAAQ,mEAAoE2C,IAUlG,YARqB,IAAhBxC,EAAKG,aAAoC,IAAfH,EAAKK,QACnCL,EAAKG,QAAS,GAEVH,EAAKG,OACTH,EAAKK,MAAQ,EACDL,EAAKK,QACjBL,EAAKK,MAAQU,GAEP2B,EAaP,SAASA,EAAazB,EAAY0B,EAASvB,GAC1C,IAAMN,EAAcG,GACnB,MAAM,IAAIb,UAAWP,EAAQ,sEAAuEoB,IAErG,IAAMJ,EAAYO,GACjB,MAAM,IAAIhB,UAAWP,EAAQ,mEAAoEuB,IAElG,OAAOf,EAAOY,EAAY0B,EAAS3C,EAAMwC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,QCtIX0B,EAAUvD,EAyEd,IC9BAqD,EDoBA,SAAsBzB,EAAY0B,EAAS1C,EAASkB,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOgB,EAAS3C,EAAT2C,CAAoB3B,EAAY0B,EAASxB,GAEjDyB,EAAS3C,EAASkB,EAAlByB,CAAyB3B,EAAY0B,EAASvB,ICvB/CwB,EAAAnD,EAFAJ,EAOAqD,EAAA,UAAAE,GAKA,IAAAC,EAAAH"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 99cb65c..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From a76b8e19d4251eb8a956ca0b1b79a0a5dd72ccc5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Jul 2022 18:41:02 +0000 Subject: [PATCH 004/138] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 681 ------ .github/workflows/publish.yml | 157 -- .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 -- branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 -- examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 -- lib/index.js | 77 - lib/limit.js | 144 -- lib/reduce.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 2689 +++++++++++++++++++++ test/test.factory.js | 1080 --------- test/test.js | 38 - test/test.reduce.js | 1020 -------- test/test.validate.js | 181 -- 44 files changed, 2720 insertions(+), 6547 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/reduce.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.reduce.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 4a2831a..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-07-01T01:08:35.383Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 128c22e..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,681 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the repository: - push: - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs rm -rf - - git add -A - git commit -m "Remove files" - - git merge -s recursive -X theirs origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch or create new branch tag: - - name: 'Push changes to `deno` branch or create new branch tag' - run: | - SLUG=${{ github.repository }} - VERSION=$(echo ${{ github.ref }} | sed -E -n 's/refs\/tags\/?(v[0-9]+.[0-9]+.[0-9]+).*/\1/p') - if [ -z "$VERSION" ]; then - echo "Workflow job was not triggered by a new tag...." - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - else - echo "Workflow job was triggered by a new tag: $VERSION" - echo "Creating new bundle branch tag of the form $VERSION-deno" - git tag -a $VERSION-deno -m "$VERSION-deno" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" $VERSION-deno - fi - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs rm -rf - - git add -A - git commit -m "Remove files" - - git merge -s recursive -X theirs origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..dc89598 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..3214704 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index ac28116..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var reduceAsync = require( './reduce.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( reduceAsync, 'factory', factory ); - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/reduce.js b/lib/reduce.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/reduce.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..176714c --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.reduce.js b/test/test.reduce.js deleted file mode 100644 index 02d2229..0000000 --- a/test/test.reduce.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib/reduce.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From e1b0bfe6ccc0ae512649b1117564d65fe7490df3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 5 Jul 2022 12:45:34 +0000 Subject: [PATCH 005/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From 27fa9a932bf0871b60ea523d914f6fc3b2522e7a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 5 Jul 2022 13:23:20 +0000 Subject: [PATCH 006/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index dc89598..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 3214704..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 176714c..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 5dfe4ae1b29f11d29011feafaed99a66400de0bc Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 5 Jul 2022 13:23:59 +0000 Subject: [PATCH 007/138] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 687 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 -- branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 -- examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 -- lib/index.js | 77 - lib/limit.js | 144 -- lib/reduce.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 2689 +++++++++++++++++++++ test/test.factory.js | 1080 --------- test/test.js | 38 - test/test.reduce.js | 1020 -------- test/test.validate.js | 181 -- 43 files changed, 2720 insertions(+), 6512 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/reduce.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.reduce.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 6726965..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,687 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the repository: - push: - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch or create new branch tag: - - name: 'Push changes to `deno` branch or create new branch tag' - run: | - SLUG=${{ github.repository }} - VERSION=$(echo ${{ github.ref }} | sed -E -n 's/refs\/tags\/?(v[0-9]+.[0-9]+.[0-9]+).*/\1/p') - if [ -z "$VERSION" ]; then - echo "Workflow job was not triggered by a new tag...." - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - else - echo "Workflow job was triggered by a new tag: $VERSION" - echo "Creating new bundle branch tag of the form $VERSION-deno" - git tag -a $VERSION-deno -m "$VERSION-deno" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" $VERSION-deno - fi - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..dc89598 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..3214704 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index ac28116..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var reduceAsync = require( './reduce.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( reduceAsync, 'factory', factory ); - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/reduce.js b/lib/reduce.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/reduce.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..f0f61e5 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.reduce.js b/test/test.reduce.js deleted file mode 100644 index 02d2229..0000000 --- a/test/test.reduce.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib/reduce.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 696573c1bfd217e7e7fd698d35d2aca191e08e0d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 6 Jul 2022 15:11:11 +0000 Subject: [PATCH 008/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From d9abdb5553731c25c9db0970b6ef1c51511ca16d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 6 Jul 2022 15:11:35 +0000 Subject: [PATCH 009/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index dc89598..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 3214704..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index f0f61e5..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 8a9d03dfb4d938de088be01efc9a1296fdffe3e8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 6 Jul 2022 15:12:13 +0000 Subject: [PATCH 010/138] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 687 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 -- branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 -- examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 -- lib/index.js | 77 - lib/limit.js | 144 -- lib/reduce.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 2689 +++++++++++++++++++++ test/test.factory.js | 1080 --------- test/test.js | 38 - test/test.reduce.js | 1020 -------- test/test.validate.js | 181 -- 44 files changed, 2720 insertions(+), 6513 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/reduce.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.reduce.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 8b13789..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ - diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 6726965..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,687 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the repository: - push: - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch or create new branch tag: - - name: 'Push changes to `deno` branch or create new branch tag' - run: | - SLUG=${{ github.repository }} - VERSION=$(echo ${{ github.ref }} | sed -E -n 's/refs\/tags\/?(v[0-9]+.[0-9]+.[0-9]+).*/\1/p') - if [ -z "$VERSION" ]; then - echo "Workflow job was not triggered by a new tag...." - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - else - echo "Workflow job was triggered by a new tag: $VERSION" - echo "Creating new bundle branch tag of the form $VERSION-deno" - git tag -a $VERSION-deno -m "$VERSION-deno" - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" $VERSION-deno - fi - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..dc89598 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..3214704 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index ac28116..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var reduceAsync = require( './reduce.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( reduceAsync, 'factory', factory ); - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/reduce.js b/lib/reduce.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/reduce.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6a0f588 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.reduce.js b/test/test.reduce.js deleted file mode 100644 index 02d2229..0000000 --- a/test/test.reduce.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib/reduce.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From f6e8ca82dce32100a4ebf3c259db16e0d232ed68 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Aug 2022 03:50:25 +0000 Subject: [PATCH 011/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From bd23de458325ba2005040ae7863f511e4b652564 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Aug 2022 11:12:08 +0000 Subject: [PATCH 012/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index dc89598..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 3214704..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6a0f588..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 76c4de16a05ae78b52c329103d80f37a358d4d88 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Aug 2022 11:12:56 +0000 Subject: [PATCH 013/138] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 33 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 -- branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 -- examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 -- lib/index.js | 77 - lib/limit.js | 144 -- lib/reduce.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 2689 +++++++++++++++++++++ test/test.factory.js | 1080 --------- test/test.js | 38 - test/test.reduce.js | 1020 -------- test/test.validate.js | 181 -- 44 files changed, 2720 insertions(+), 6586 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/reduce.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.reduce.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7212d81..0000000 --- a/.gitattributes +++ /dev/null @@ -1,33 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 6f0a299..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-08-01T01:11:48.479Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 5094681..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c9c0d1d --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..63d58af --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index ac28116..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var reduceAsync = require( './reduce.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( reduceAsync, 'factory', factory ); - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/reduce.js b/lib/reduce.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/reduce.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..2273d64 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.reduce.js b/test/test.reduce.js deleted file mode 100644 index 02d2229..0000000 --- a/test/test.reduce.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib/reduce.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 585e871d4d3bfa19fcce5d13ea3258a0bb661db2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Sep 2022 03:39:54 +0000 Subject: [PATCH 014/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From f45f999a7b7dcaeb5079d07ac1693c7406cf9ea0 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Sep 2022 13:52:09 +0000 Subject: [PATCH 015/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c9c0d1d..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 63d58af..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 2273d64..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 558a1cf6464edb6b49d15f84c6419f56f335b33b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Sep 2022 13:52:52 +0000 Subject: [PATCH 016/138] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 -- branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 -- examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 -- lib/index.js | 77 - lib/limit.js | 144 -- lib/reduce.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 2689 +++++++++++++++++++++ test/test.factory.js | 1080 --------- test/test.js | 38 - test/test.reduce.js | 1020 -------- test/test.validate.js | 181 -- 44 files changed, 2720 insertions(+), 6602 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/reduce.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.reduce.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index d2e9e67..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-09-01T01:09:32.250Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 5094681..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c9c0d1d --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..63d58af --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index ac28116..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var reduceAsync = require( './reduce.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( reduceAsync, 'factory', factory ); - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/reduce.js b/lib/reduce.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/reduce.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..9fe1fbd --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.reduce.js b/test/test.reduce.js deleted file mode 100644 index 02d2229..0000000 --- a/test/test.reduce.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib/reduce.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From e0aa268a95166977deb2d50814d8b9b3754fc081 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 17 Sep 2022 19:34:22 +0000 Subject: [PATCH 017/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From a51bd668da26a78c54ea5035f504b77a32dbd9f1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 17 Sep 2022 19:48:09 +0000 Subject: [PATCH 018/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c9c0d1d..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 63d58af..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/reduce.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport reduceAsync from './reduce.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( reduceAsync, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAH,EAAA,UAAAN"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 9fe1fbd..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 0eb7d0cdc256961125b37c3e9af3cd79d7f5dce8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 17 Sep 2022 19:49:03 +0000 Subject: [PATCH 019/138] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 -- branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 -- examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 -- lib/index.js | 77 - lib/limit.js | 144 -- lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 2689 +++++++++++++++++++++ test/test.factory.js | 1080 --------- test/test.js | 38 - test/test.main.js | 1020 -------- test/test.validate.js | 181 -- 43 files changed, 2720 insertions(+), 6601 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 5094681..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c9c0d1d --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..bc36c71 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..29bb63e --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From aa7b0c52ef1c07d64698382ab636e54e4fe19832 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Oct 2022 04:44:12 +0000 Subject: [PATCH 020/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From 9af595c2b66c49120e0fe2e118801e36a39cd1ef Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Oct 2022 19:37:17 +0000 Subject: [PATCH 021/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c9c0d1d..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index bc36c71..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 29bb63e..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 6482d4786aa43ca6826cf87a27bf85627bb013d3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Oct 2022 19:37:56 +0000 Subject: [PATCH 022/138] Auto-generated commit --- .editorconfig | 181 -- .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ------ .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 -- .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 -- .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 ---- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 -- branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 -- examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 -- lib/index.js | 77 - lib/limit.js | 144 -- lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 2689 +++++++++++++++++++++ test/test.factory.js | 1080 --------- test/test.js | 38 - test/test.main.js | 1020 -------- test/test.validate.js | 181 -- 44 files changed, 2720 insertions(+), 6602 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 31d3d1e..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-10-01T01:34:23.657Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 29bf533..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a7a7f51..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.9.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 39b1613..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 7ca169c..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '0 8 * * 6' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "::set-output name=package_name::$name" - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "::set-output name=data::$data" - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v2 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v2 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 5094681..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "::set-output name=remote-exists::true" - else - echo "::set-output name=remote-exists::false" - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v2 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "::set-output name=alias::${alias}" - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..c9c0d1d --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..bc36c71 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6ec8308 --- /dev/null +++ b/stats.html @@ -0,0 +1,2689 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From c3fc87f2edd38fba878829cbaa6463eccc239102 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Nov 2022 04:21:17 +0000 Subject: [PATCH 023/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From 880e0181556794f2f430d139fb0f6d36392fe0a8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Nov 2022 18:56:53 +0000 Subject: [PATCH 024/138] Remove files --- index.d.ts | 411 -------- index.mjs | 4 - index.mjs.map | 1 - stats.html | 2689 ------------------------------------------------- 4 files changed, 3105 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index c9c0d1d..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index bc36c71..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;g2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,QC7BrCU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,KA3BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,GAgCnE,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,ICrCF,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,GACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,OAEJqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,OCvEf,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,GClB/C2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6ec8308..0000000 --- a/stats.html +++ /dev/null @@ -1,2689 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 381187dccb32f3aaace4e9e30d72f7ac01f24b79 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Nov 2022 18:57:42 +0000 Subject: [PATCH 025/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 760 ---- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 - .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 4044 +++++++++++++++++++++ test/test.factory.js | 1080 ------ test/test.js | 38 - test/test.main.js | 1020 ------ test/test.validate.js | 181 - 44 files changed, 4075 insertions(+), 6602 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index e647f4c..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-11-01T01:22:38.932Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9113bfe..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,760 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8e8c7c4 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.7-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..d1b12c1 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,KC/CIU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,EAIA,CACF,CC1CA,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,CAAA,EACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,CACN,MACEqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..694ad6b --- /dev/null +++ b/stats.html @@ -0,0 +1,4044 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 2029911dc0cb1587f55f010a291206c3d01c9d92 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 4 Nov 2022 00:08:00 +0000 Subject: [PATCH 026/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index e8f32b4..1d03472 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From 3cc67a5c9012939d4294f3b1b9593afe537b57c4 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 4 Nov 2022 13:24:23 +0000 Subject: [PATCH 027/138] Remove files --- index.d.ts | 411 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4044 ------------------------------------------------- 4 files changed, 4460 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8e8c7c4..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import i from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import e from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.7-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(i,e){return n(e)?(o(e,"thisArg")&&(i.thisArg=e.thisArg),o(e,"series")&&(i.series=e.series,!l(i.series))?new TypeError(s("0ie30","series",i.series)):o(e,"limit")&&(i.limit=e.limit,!m(i.limit))?new TypeError(s("0ie3b","limit",i.limit)):null):new TypeError(s("0ie2h",e))}var f=()=>{};function h(i,e,t,s,r){var n,o,l,m,d,h,p;if(0===(d=i.length))return r(null,e);for(m=d1){if(l=d(o,i))throw l;m=n}else m=i;if(!e(m))throw new TypeError(s("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),f;function f(i,r,n){if(!t(i))throw new TypeError(s("0ieBO",i));if(!e(n))throw new TypeError(s("0ie43",n));return h(i,r,o,m,(function(i,e){if(i)return n(i);n(null,e)}))}}function c(i,e,t,s,r){if(arguments.length<5)return p(t)(i,e,s);p(t,s)(i,e,r)}i(c,"factory",p);export{c as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index d1b12c1..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport logger from 'debug' ;\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,KC/CIU,SAkBJ,SAASF,EAAOG,EAAYC,EAAKb,EAAMc,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAAa,KAHbF,EAAMR,EAAWW,QAKhB,OAAOR,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMpB,EAAKS,MACTW,EAEApB,EAAKS,MAGZE,EAAO,0BAA2BG,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdf,EAAO,kBAAmBkB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDjB,EAAO,6BADPU,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAM/B,EAAKI,QAASS,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJf,EAAO,2BAA4Be,EAAMM,SAClCjB,EAAMW,KAEdT,GAAS,EAEJI,EAAML,EACHQ,IAEHP,IAAUG,EAEPL,EAAM,KAAMF,QAFpB,EAIA,CACF,CC1CA,SAASoB,EAAShC,EAASa,GAC1B,IAAId,EACAkC,EACAC,EAGJ,GADAnC,EAAO,CAAA,EACFoC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMnC,EAAUC,EAAMC,GAErB,MAAMiC,EAEPC,EAAIrB,CACN,MACEqB,EAAIlC,EAEL,IAAMoC,EAAYF,GACjB,MAAM,IAAI5B,UAAWC,EAAQ,QAAS2B,IAUvC,YARqB,IAAhBnC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ6B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIL,UAAWC,EAAQ,QAASI,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIR,UAAWC,EAAQ,QAASO,IAEvC,OAAON,EAAOG,EAAY4B,EAASxC,EAAMmC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASvC,EAASa,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAAShC,EAATgC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAAShC,EAASa,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 694ad6b..0000000 --- a/stats.html +++ /dev/null @@ -1,4044 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From da565fdb3307f3562863b0e4671e19bb8836c34d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 4 Nov 2022 13:25:08 +0000 Subject: [PATCH 028/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 781 ---- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 178 - .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 4044 +++++++++++++++++++++ test/test.factory.js | 1080 ------ test/test.js | 38 - test/test.main.js | 1020 ------ test/test.validate.js | 181 - 44 files changed, 4075 insertions(+), 6623 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index e6fdda8..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-11-03T21:57:07.695Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 37ddb4f..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,781 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..f37b089 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){var t=e.default;if("function"==typeof t){var i=function(){return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,m,d,c,p;if(d=e.length,f("Collection length: %d",d),0===d)return f("Finished processing a collection."),s(null,t);for(m=d1){if(l=d(o,e))throw l;m=n}else m=e;if(!t(m))throw new TypeError(r("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,m,(function(e,t){if(e)return n(e);n(null,t)}))}}function h(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(h,"factory",u);export{h as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..b9ecb68 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,0WC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 1d03472..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-spec": "5.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..871789b --- /dev/null +++ b/stats.html @@ -0,0 +1,4044 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 80b649c7e80f313590b00893ad05de9daa3f399b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Dec 2022 03:59:37 +0000 Subject: [PATCH 029/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 46ebc71..4d2fff3 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From c2589499d69e0b7d2694604aa94eff7bfaf5387c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Dec 2022 11:27:23 +0000 Subject: [PATCH 030/138] Remove files --- index.d.ts | 411 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4044 ------------------------------------------------- 4 files changed, 4460 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index f37b089..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){var t=e.default;if("function"==typeof t){var i=function(){return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,m,d,c,p;if(d=e.length,f("Collection length: %d",d),0===d)return f("Finished processing a collection."),s(null,t);for(m=d1){if(l=d(o,e))throw l;m=n}else m=e;if(!t(m))throw new TypeError(r("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,m,(function(e,t){if(e)return n(e);n(null,t)}))}}function h(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(h,"factory",u);export{h as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index b9ecb68..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object' ;\nimport hasOwnProp from '@stdlib/assert-has-own-property' ;\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean' ;\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function' ;\nimport isCollection from '@stdlib/assert-is-collection' ;\nimport format from '@stdlib/error-tools-fmtprodmsg' ;\nimport PINF from '@stdlib/constants-float64-pinf' ;\nimport validate from './validate.js' ;\nimport limit from './limit.js' ;\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file' ;\n* import reduceAsync from '@stdlib/utils-async-reduce' ;\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property' ;\nimport main from './main.js' ;\nimport factory from './factory.js' ;\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,0WC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 871789b..0000000 --- a/stats.html +++ /dev/null @@ -1,4044 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 9b673f01d807f6c019d717c0f89689e4ed20946d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Dec 2022 11:27:57 +0000 Subject: [PATCH 031/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 781 ---- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 183 - .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 4044 +++++++++++++++++++++ test/test.factory.js | 1080 ------ test/test.js | 38 - test/test.main.js | 1020 ------ test/test.validate.js | 181 - 44 files changed, 4075 insertions(+), 6628 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index e8a137e..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2022-12-01T01:46:57.977Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 37ddb4f..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,781 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..f37b089 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){var t=e.default;if("function"==typeof t){var i=function(){return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,m,d,c,p;if(d=e.length,f("Collection length: %d",d),0===d)return f("Finished processing a collection."),s(null,t);for(m=d1){if(l=d(o,e))throw l;m=n}else m=e;if(!t(m))throw new TypeError(r("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,m,(function(e,t){if(e)return n(e);n(null,t)}))}}function h(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(h,"factory",u);export{h as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..e5ecb10 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,0WC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 4d2fff3..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "2.x.x" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..49f8d5e --- /dev/null +++ b/stats.html @@ -0,0 +1,4044 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 1217dc162e592e1e060501a89606c36703f1656c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Jan 2023 02:55:17 +0000 Subject: [PATCH 032/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index ba48f53..be3957d 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From 88d41d95fc08358cd2ec38339c600ef144c7d49e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Jan 2023 12:30:04 +0000 Subject: [PATCH 033/138] Remove files --- index.d.ts | 411 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4044 ------------------------------------------------- 4 files changed, 4460 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index f37b089..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2022 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){var t=e.default;if("function"==typeof t){var i=function(){return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,m,d,c,p;if(d=e.length,f("Collection length: %d",d),0===d)return f("Finished processing a collection."),s(null,t);for(m=d1){if(l=d(o,e))throw l;m=n}else m=e;if(!t(m))throw new TypeError(r("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,m,(function(e,t){if(e)return n(e);n(null,t)}))}}function h(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(h,"factory",u);export{h as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index e5ecb10..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,0WC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 49f8d5e..0000000 --- a/stats.html +++ /dev/null @@ -1,4044 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From b73a03881e1c3fbd4b7d5f1248d2f77b1cbe65c4 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Jan 2023 12:30:45 +0000 Subject: [PATCH 034/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 791 ---- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 184 - .npmignore | 227 -- .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 -- docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 4044 +++++++++++++++++++++ test/test.factory.js | 1080 ------ test/test.js | 38 - test/test.main.js | 1020 ------ test/test.validate.js | 181 - 44 files changed, 4075 insertions(+), 6639 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 151f35d..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-01-01T01:12:43.090Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4eea88..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,791 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..172dc3f --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){var t=e.default;if("function"==typeof t){var i=function(){return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,m,d,c,p;if(d=e.length,f("Collection length: %d",d),0===d)return f("Finished processing a collection."),s(null,t);for(m=d1){if(l=d(o,e))throw l;m=n}else m=e;if(!t(m))throw new TypeError(r("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,m,(function(e,t){if(e)return n(e);n(null,t)}))}}function h(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(h,"factory",u);export{h as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..c6f6615 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,0WC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index be3957d..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..278b841 --- /dev/null +++ b/stats.html @@ -0,0 +1,4044 @@ + + + + + + + + RollUp Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 7c903c456c32000f341bf17450a1115fa33dafd5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Feb 2023 03:49:16 +0000 Subject: [PATCH 035/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index ba48f53..be3957d 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.x", "@stdlib/assert-is-positive-integer": "^0.0.x", "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/string-format": "^0.0.x", + "@stdlib/error-tools-fmtprodmsg": "^0.0.x", "@stdlib/types": "^0.0.x", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", "debug": "^2.6.9" From 17f2b8a6ed15337b2ca7e7ee9283a37230286e74 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Feb 2023 12:06:12 +0000 Subject: [PATCH 036/138] Remove files --- index.d.ts | 411 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4044 ------------------------------------------------- 4 files changed, 4460 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 172dc3f..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function d(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){var t=e.default;if("function"==typeof t){var i=function(){return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,m,d,c,p;if(d=e.length,f("Collection length: %d",d),0===d)return f("Finished processing a collection."),s(null,t);for(m=d1){if(l=d(o,e))throw l;m=n}else m=e;if(!t(m))throw new TypeError(r("0ie43",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,m,(function(e,t){if(e)return n(e);n(null,t)}))}}function h(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(h,"factory",u);export{h as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index c6f6615..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;y1BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,0WC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 278b841..0000000 --- a/stats.html +++ /dev/null @@ -1,4044 +0,0 @@ - - - - - - - - RollUp Visualizer - - - -
- - - - - From 27d835d481f714d22e76a1d33008e5278118faed Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Feb 2023 12:07:01 +0000 Subject: [PATCH 037/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 791 --- .github/workflows/publish.yml | 117 - .github/workflows/test.yml | 92 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 184 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 44 files changed, 6208 insertions(+), 6639 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index f663f78..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-02-01T01:35:38.438Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4eea88..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,791 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\"/\"@stdlib\/error-tools-fmtprodmsg\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^0.0.x'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index f9298cf..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..921c5ae --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index be3957d..edba815 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.x", - "@stdlib/assert-is-boolean": "^0.0.x", - "@stdlib/assert-is-collection": "^0.0.x", - "@stdlib/assert-is-function": "^0.0.x", - "@stdlib/assert-is-plain-object": "^0.0.x", - "@stdlib/assert-is-positive-integer": "^0.0.x", - "@stdlib/constants-float64-pinf": "^0.0.x", - "@stdlib/error-tools-fmtprodmsg": "^0.0.x", - "@stdlib/types": "^0.0.x", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.x", - "@stdlib/constants-float64-eps": "^0.0.x", - "@stdlib/fs-read-file": "^0.0.x", - "@stdlib/utils-noop": "^0.0.x", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..44fdfb9 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 019b38b17e77a354016507e26a76ccbc591a8872 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Mar 2023 05:57:19 +0000 Subject: [PATCH 038/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From 78f37163822efe5632bc58dbeb0942360c65cc35 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Mar 2023 14:31:58 +0000 Subject: [PATCH 039/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 921c5ae..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 44fdfb9..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 0ad4a7177444fa1e38039adcab560caff700a96c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Mar 2023 14:32:44 +0000 Subject: [PATCH 040/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 236 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 44 files changed, 6208 insertions(+), 6774 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 3c78cbc..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-03-01T03:51:57.139Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..921c5ae --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..441e97a --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From bfb2e297adb85233bf3b0f94525ba83029a9297d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Apr 2023 06:33:12 +0000 Subject: [PATCH 041/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From c23785c53c5aa3197a62441e6e0bab98fed12c7e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Apr 2023 13:34:13 +0000 Subject: [PATCH 042/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 921c5ae..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 441e97a..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From bde04c385484327b16994e85906501a128d50f1a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Apr 2023 13:35:06 +0000 Subject: [PATCH 043/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 44 files changed, 6208 insertions(+), 6780 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 6e09c3c..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-04-01T04:24:47.567Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..921c5ae --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b6029ed --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From e05bce6941da12eebf17b3cd03577b12182dd52a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 May 2023 06:18:15 +0000 Subject: [PATCH 044/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From a16a7224721f4bb90fb290aea8991437027ddbd3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 May 2023 13:15:23 +0000 Subject: [PATCH 045/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 921c5ae..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b6029ed..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 5eed51a88f5f910ad47439068726bd913ab98b54 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 May 2023 13:16:11 +0000 Subject: [PATCH 046/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 44 files changed, 6208 insertions(+), 6780 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 57a3d59..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-05-01T04:11:16.829Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..921c5ae --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..91b3f5b --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 3443ece01974ae861c7081ca54210479288586b2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Jun 2023 05:57:21 +0000 Subject: [PATCH 047/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From a640aefb9c58dd0c00ec7e87495a068d095cf102 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Jun 2023 13:32:53 +0000 Subject: [PATCH 048/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 921c5ae..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 91b3f5b..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From ed1863325dd4455334e7691404eef0a4c513d74b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Jun 2023 13:33:34 +0000 Subject: [PATCH 049/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 44 files changed, 6208 insertions(+), 6780 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 4bc10c0..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-06-01T03:51:37.482Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..921c5ae --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..acb4b6e --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 82a76779374d1b0f4c2fc8c958106e882c96f1f6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jul 2023 05:59:35 +0000 Subject: [PATCH 050/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From c6a63c9e4b2ec2148cbe22ef1ecc7a7f3362c935 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jul 2023 13:46:57 +0000 Subject: [PATCH 051/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 921c5ae..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index acb4b6e..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From e9a30fed004ba4ee74f088a4e5a7b5813db4b4cb Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 1 Jul 2023 13:47:37 +0000 Subject: [PATCH 052/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 44 files changed, 6208 insertions(+), 6780 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 2fd46e7..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-07-01T03:52:18.754Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 3e8e2db..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v1 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..921c5ae --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..9dedaa1 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 6664267f025bc611ba9a069e49b2ce9d0d23994b Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Aug 2023 06:27:24 +0000 Subject: [PATCH 053/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 357a2e5..659543d 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -110,7 +110,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -135,10 +135,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From 42a7183ed34e141c32f9c57f9e309de13f81394f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Aug 2023 14:49:50 +0000 Subject: [PATCH 054/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 921c5ae..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 9dedaa1..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 200450f633a4c2097089f2dbd09e9fa0c885070a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Aug 2023 14:50:36 +0000 Subject: [PATCH 055/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 165 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 96 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 44 files changed, 6208 insertions(+), 6780 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 609680b..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-08-01T04:15:09.989Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 334eb59..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..921c5ae --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 659543d..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,165 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index 2dee2f0..0000000 --- a/lib/main.js +++ /dev/null @@ -1,96 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..e588fa9 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From fbfce080ae7b146b6911d4cddb696a136a222dba Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 2 Aug 2023 16:47:59 +0000 Subject: [PATCH 056/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..dc4fbb6 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '0ie43', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '0ieBO', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '0ie43', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25df3e8 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '0ie2h', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '0ie30', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From efcfd35c869795d253ee86b435162033f1d41b45 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 2 Aug 2023 16:53:04 +0000 Subject: [PATCH 057/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 921c5ae..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC1CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CCnBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index e588fa9..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 66523eae509cd15fba9a423f33d1af1b8df8806e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 2 Aug 2023 16:53:40 +0000 Subject: [PATCH 058/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 798 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 43 files changed, 6208 insertions(+), 6777 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 0fd4d6c..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 334eb59..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,798 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Format error messages: - - name: 'Replace double quotes with single quotes in rewritten format string error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\( \"([a-zA-Z0-9]+)\"/Error\( format\( '\1'/g" {} \; - - # Format string literal error messages: - - name: 'Replace double quotes with single quotes in rewritten string literal error messages' - run: | - find . -name "*.js" -exec sed -E -i "s/Error\( format\(\"([a-zA-Z0-9]+)\"\)/Error\( format\( '\1' \)/g" {} \; - - # Format code: - - name: 'Replace double quotes with single quotes in inserted `require` calls' - run: | - find . -name "*.js" -exec sed -E -i "s/require\( ?\"@stdlib\/error-tools-fmtprodmsg\" ?\);/require\( '@stdlib\/error-tools-fmtprodmsg' \);/g" {} \; - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -447,7 +446,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -515,11 +514,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index db63b78..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index a2d50e3..d1fd90f 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 2.0 -/// +/// import { Collection } from '@stdlib/types/object'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a7ded05 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..32ff1d3 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index dc4fbb6..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '0ie43', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '0ieBO', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '0ie43', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25df3e8..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '0ie2h', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '0ie30', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '0ie3b', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b5e2e33 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From d09b047a761edc1e400dc6b58bd8b82269137d6e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 20 Sep 2023 23:09:53 +0000 Subject: [PATCH 059/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 5bc39be..fcaa738 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.0.7", "@stdlib/assert-is-positive-integer": "^0.0.7", "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/string-format": "^0.0.3", + "@stdlib/error-tools-fmtprodmsg": "^0.0.2", "@stdlib/types": "^0.0.14", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", "debug": "^2.6.9" From d87153161f3d3231db5a880c108759039c12569c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 20 Sep 2023 23:12:41 +0000 Subject: [PATCH 060/138] Remove files --- index.d.ts | 411 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6593 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index d1fd90f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,411 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 2.0 - -/// - -import { Collection } from '@stdlib/types/object'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true). - */ - series?: boolean; - - /** - * Execution context. - */ - thisArg?: any; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: any ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( accumulator: any, value: any, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( accumulator: any, value: any, index: number, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( accumulator: any, value: any, index: number, collection: Collection, next: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: any, done: Callback ) => void; // tslint-disable-line max-line-length - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: any, options: Options, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: any, reducer: Reducer, done: Callback ): void; // tslint-disable-line max-line-length - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a7ded05..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("0ie30","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("0ie3b","limit",e.limit)):null):new TypeError(r("0ie2h",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("0ie43",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("0ieBO",e));if(!t(n))throw new TypeError(r("0ie43",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 32ff1d3..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '0ie2h', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '0ie30', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '0ie3b', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '0ie43', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '0ieBO', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '0ie43', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUR,EAAKK,SAGnDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASR,EAAKS,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASP,GAkBzC,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,QAAS4B,IAUvC,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,QAASK,IAEvC,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b5e2e33..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From a4d87581253839efc266a9e9c77a0a38a0ab351d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 20 Sep 2023 23:13:24 +0000 Subject: [PATCH 061/138] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 783 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 47 files changed, 6208 insertions(+), 6818 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 91f2b93..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,783 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 18e6be2..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 4e59aa1..aa2fd91 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..880df91 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..bf94f03 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index fcaa738..cc2d2a3 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.0.8", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.0.7", - "@stdlib/assert-is-boolean": "^0.0.8", - "@stdlib/assert-is-collection": "^0.0.8", - "@stdlib/assert-is-function": "^0.0.8", - "@stdlib/assert-is-plain-object": "^0.0.7", - "@stdlib/assert-is-positive-integer": "^0.0.7", - "@stdlib/constants-float64-pinf": "^0.0.8", - "@stdlib/error-tools-fmtprodmsg": "^0.0.2", - "@stdlib/types": "^0.0.14", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.7", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.0.12", - "@stdlib/constants-float64-eps": "^0.0.8", - "@stdlib/fs-read-file": "^0.0.8", - "@stdlib/utils-noop": "^0.0.14", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..1063f80 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 7ae7c4ca1c7f521a94d37f6e0f19c4276e3cda21 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 23 Sep 2023 16:11:56 +0000 Subject: [PATCH 062/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 88d7186..d4f288f 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.0", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.0", - "@stdlib/string-format": "^0.1.0", + "@stdlib/error-tools-fmtprodmsg": "^0.1.0", "@stdlib/types": "^0.1.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.0", "debug": "^2.6.9" From cb677be319f2ebc77ba5e0225994c5f2f29e0209 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 23 Sep 2023 17:37:30 +0000 Subject: [PATCH 063/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index aa2fd91..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; // tslint:disable-line:no-unnecessary-generics - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; // tslint:disable-line:no-unnecessary-generics -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 880df91..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.0.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.0.8-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index bf94f03..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;u2BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 1063f80..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 1a79bfa28993164da838f4af326b966ef5c4891a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 23 Sep 2023 17:38:12 +0000 Subject: [PATCH 064/138] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 62 - .github/workflows/cancel.yml | 56 - .github/workflows/close_pull_requests.yml | 44 - .github/workflows/examples.yml | 62 - .github/workflows/npm_downloads.yml | 108 - .github/workflows/productionize.yml | 783 --- .github/workflows/publish.yml | 242 - .github/workflows/test.yml | 97 - .github/workflows/test_bundles.yml | 180 - .github/workflows/test_coverage.yml | 123 - .github/workflows/test_install.yml | 83 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 47 files changed, 6208 insertions(+), 6818 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 06a9a75..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index a00dbe5..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,56 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - uses: styfle/cancel-workflow-action@0.11.0 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 00b6c5b..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,44 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 7902a7d..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,62 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout the repository' - uses: actions/checkout@v3 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 82b2b3b..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,108 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - uses: actions/upload-artifact@v3 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - uses: distributhor/workflow-webhook@v3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 91f2b93..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,783 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - uses: actions/checkout@v3 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/checkout@v3 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - uses: act10ns/slack@v2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - uses: actions/checkout@v3 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - uses: actions/setup-node@v3 - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 18e6be2..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 4e59aa1..aa2fd91 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..44ead5a --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.0-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.0-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.0-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..4a864da --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;04BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index d4f288f..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.0", - "@stdlib/assert-is-boolean": "^0.1.0", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.0", - "@stdlib/assert-is-plain-object": "^0.1.0", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.0", - "@stdlib/error-tools-fmtprodmsg": "^0.1.0", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.0", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.1.0", - "@stdlib/constants-float64-eps": "^0.1.0", - "@stdlib/fs-read-file": "^0.1.0", - "@stdlib/utils-noop": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..a1a09a7 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From e2d5921258dac46d1387163a97a788b6279582a3 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 23 Sep 2023 18:08:35 +0000 Subject: [PATCH 065/138] Update README.md for ESM bundle v0.1.0 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9668a31..5d1fc18 100644 --- a/README.md +++ b/README.md @@ -52,13 +52,13 @@ limitations under the License. ## Usage ```javascript -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.1.0-esm/index.mjs'; ``` You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.1.0-esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +381,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.1.0-esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From b69a4be0fcb5eaf300b6dcf004adaf91ae3e6d1d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 23 Sep 2023 18:08:36 +0000 Subject: [PATCH 066/138] Auto-generated commit --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5d1fc18..8bb2315 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/utils-async-reduce/tags). For example, + ```javascript import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.1.0-esm/index.mjs'; ``` @@ -58,7 +63,7 @@ import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduc You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.1.0-esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +386,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.1.0-esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From f58c4c0e67d6954d20a1e6ee78d6f1a59f163f9d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Oct 2023 07:57:50 +0000 Subject: [PATCH 067/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 88d7186..d4f288f 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.0", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.0", - "@stdlib/string-format": "^0.1.0", + "@stdlib/error-tools-fmtprodmsg": "^0.1.0", "@stdlib/types": "^0.1.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.0", "debug": "^2.6.9" From e8c0f60c0c7eed0fe248e04e5b45a509dcef7838 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 2 Oct 2023 12:45:25 +0000 Subject: [PATCH 068/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index aa2fd91..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; // tslint:disable-line:no-unnecessary-generics - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; // tslint:disable-line:no-unnecessary-generics -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 44ead5a..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.0-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.0-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.0-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 4a864da..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;04BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index a1a09a7..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 05d8cb4b298ebdbb32060fbbdc2241742aa4750a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 2 Oct 2023 12:46:10 +0000 Subject: [PATCH 069/138] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 247 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 38 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 49 files changed, 6208 insertions(+), 6915 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 862f25f..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-10-01T02:49:40.817Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b031f39..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 18e6be2..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 4e59aa1..aa2fd91 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..fb44658 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.0-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.0-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.0-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..4ae2f14 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index d4f288f..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.0", - "@stdlib/assert-is-boolean": "^0.1.0", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.0", - "@stdlib/assert-is-plain-object": "^0.1.0", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.0", - "@stdlib/error-tools-fmtprodmsg": "^0.1.0", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.0", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.1.0", - "@stdlib/constants-float64-eps": "^0.1.0", - "@stdlib/fs-read-file": "^0.1.0", - "@stdlib/utils-noop": "^0.1.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..7348846 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index b8fcc51..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From f17803f195ac83b91153ad187efdb8a66fa0e0a5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 5 Oct 2023 11:17:21 +0000 Subject: [PATCH 070/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index b942271..374d8aa 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.1", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.1.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", "debug": "^2.6.9" From 9ca371ff2fefe7ebb3034a9237d7da028138b57c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 6 Oct 2023 14:52:23 +0000 Subject: [PATCH 071/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index aa2fd91..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; // tslint:disable-line:no-unnecessary-generics - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; // tslint:disable-line:no-unnecessary-generics -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index fb44658..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.0-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.0-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.0-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.0-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 4ae2f14..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 7348846..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From a0c2a0ad12753ec780dc048f633d94d345fde825 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 6 Oct 2023 14:53:06 +0000 Subject: [PATCH 072/138] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 247 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 38 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 48 files changed, 6208 insertions(+), 6914 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b031f39..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 18e6be2..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 4e59aa1..aa2fd91 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..b0c2594 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..4ae2f14 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 374d8aa..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.1.1", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.1", - "@stdlib/assert-is-plain-object": "^0.1.1", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.1.0", - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.1.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b36115a --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index b8fcc51..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 8d6775b7b419b1276a2bd4448b13c2b99c1ba603 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 1 Nov 2023 08:07:17 +0000 Subject: [PATCH 073/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index b942271..374d8aa 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.1", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.1.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", "debug": "^2.6.9" From 2dd0ed0941f2bce9c5dd8ab9f0f657e831a49a69 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 2 Nov 2023 13:35:41 +0000 Subject: [PATCH 074/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index aa2fd91..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; // tslint:disable-line:no-unnecessary-generics - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; // tslint:disable-line:no-unnecessary-generics -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index b0c2594..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 4ae2f14..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b36115a..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 3744a35c7899bdadf8591c9a58cb6821f224d1dd Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 2 Nov 2023 13:36:52 +0000 Subject: [PATCH 075/138] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 49 files changed, 6208 insertions(+), 6918 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index de4e9f4..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-11-01T02:55:45.207Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b031f39..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 18e6be2..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 4e59aa1..aa2fd91 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..b0c2594 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..4ae2f14 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 374d8aa..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.1.1", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.1", - "@stdlib/assert-is-plain-object": "^0.1.1", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.1.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.1.0", - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.1.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..d4dd1b3 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 37b5f079423722cb7294fee556c42e3e72a69cc5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 9 Nov 2023 11:41:54 +0000 Subject: [PATCH 076/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 11bedc0..a618e92 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.1", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", "debug": "^2.6.9" From e0c2096c2c3dbff13ad9b98df73ac408ed7e0a3c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 9 Nov 2023 15:26:04 +0000 Subject: [PATCH 077/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index aa2fd91..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; // tslint:disable-line:no-unnecessary-generics - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; // tslint:disable-line:no-unnecessary-generics -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index b0c2594..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 4ae2f14..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index d4dd1b3..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 9979c56e7a41a5783b59ce65d2b3f0089497c587 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 9 Nov 2023 15:26:40 +0000 Subject: [PATCH 078/138] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 48 files changed, 6208 insertions(+), 6917 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b031f39..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 18e6be2..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 4e59aa1..aa2fd91 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..b0c2594 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..4ae2f14 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index a618e92..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.1.1", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.1", - "@stdlib/assert-is-plain-object": "^0.1.1", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.2.0", - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.1.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..3f53175 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 6524530881ad379231bd8c4c74f960eed9f055f9 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 12 Nov 2023 16:04:44 +0000 Subject: [PATCH 079/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 11bedc0..a618e92 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.1", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", "debug": "^2.6.9" From 1f5067fd820c5ab2df7cd280e8dddf8a90c64d4d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 12 Nov 2023 17:29:17 +0000 Subject: [PATCH 080/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index aa2fd91..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; // tslint:disable-line:no-unnecessary-generics - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; // tslint:disable-line:no-unnecessary-generics -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index b0c2594..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 4ae2f14..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 3f53175..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From c6711987e2af0ecefc9ef7f8db83e5900c753ee2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 12 Nov 2023 17:30:00 +0000 Subject: [PATCH 081/138] Auto-generated commit --- .editorconfig | 186 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 166 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 48 files changed, 6208 insertions(+), 6917 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 13e9c39..0000000 --- a/.editorconfig +++ /dev/null @@ -1,186 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tslint.json` files: -[tslint.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b031f39..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index 18e6be2..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,166 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/* tslint:disable:no-unsafe-any */ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 416a0a3..96bbe7c 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..b0c2594 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..4ae2f14 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index a618e92..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.1.1", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.1", - "@stdlib/assert-is-plain-object": "^0.1.1", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.2.0", - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.1.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..478d478 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 7aeb710a2fdb674e0f32d9c40b08944c2beb4f86 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Dec 2023 06:57:20 +0000 Subject: [PATCH 082/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..ab0820e 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q,JV', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh,O3', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q,JV', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..25dbd5a 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V,FD', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 84fa1e6..7937721 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.1", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", "debug": "^2.6.9" From 9d61ae21217f47207180c6f41c8a2286a6d7e057 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 2 Dec 2023 01:55:29 +0000 Subject: [PATCH 083/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96bbe7c..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index b0c2594..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 4ae2f14..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 478d478..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 58fd117301f92826e22cdff8cc9b2c7731c4e848 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 2 Dec 2023 01:56:04 +0000 Subject: [PATCH 084/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 227 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 49 files changed, 6208 insertions(+), 6911 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 73d968b..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2023-12-01T03:06:27.470Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index ab56cca..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c1c45e7..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index b031f39..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA corresponding to v3.0.3: - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 265afda..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA corresponding to v4.1.0 - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 16 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2023. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b998259..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index de62e82..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 416a0a3..96bbe7c 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..b0c2594 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..4ae2f14 --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index ab0820e..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q,JV', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh,O3', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q,JV', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 25dbd5a..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V,FD', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o,GE', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 7937721..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.1.1", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.1", - "@stdlib/assert-is-plain-object": "^0.1.1", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/bench": "^0.2.1", - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.1.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..132b1f7 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 7c603e1d5205368cce219f172e8d2399eabaa645 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jan 2024 04:21:38 +0000 Subject: [PATCH 085/138] Transform error messages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 79800b4..d48276e 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.1", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", "debug": "^2.6.9" From d4f54acc22b0da1749b9f3d6030e696a933008f2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jan 2024 10:31:34 +0000 Subject: [PATCH 086/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96bbe7c..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index b0c2594..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2023 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as d}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function m(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o,GE","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!d(e.limit))?new TypeError(r("1Rb3P,Fv","limit",e.limit)):null):new TypeError(r("1Rb2V,FD",t))}function c(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var r=Function.bind.apply(t,i);return new r}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var f=c(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function p(e,t,i,r,s){var n,o,l,d,m,c,p;if(m=e.length,f("Collection length: %d",m),0===m)return f("Finished processing a collection."),s(null,t);for(d=m1){if(l=m(o,e))throw l;d=n}else d=e;if(!t(d))throw new TypeError(r("1Rb3q,JV",d));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=s),c;function c(e,s,n){if(!i(e))throw new TypeError(r("1RbAh,O3",e));if(!t(n))throw new TypeError(r("1Rb3q,JV",n));return p(e,s,o,d,(function(e,t){if(e)return n(e);n(null,t)}))}}function a(e,t,i,r,s){if(arguments.length<5)return u(i)(e,t,r);u(i,r)(e,t,s)}e(a,"factory",u);export{a as default,u as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 4ae2f14..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V,FD', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o,GE', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P,Fv', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/error-tools-fmtprodmsg';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q,JV', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh,O3', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q,JV', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;w5BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,WAAY,SAAUR,EAAKK,SAGtDF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,WAAY,QAASR,EAAKS,QAGnD,MAjBC,IAAIF,UAAWC,EAAQ,WAAYP,GAkB5C,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,WAAY4B,IAU1C,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,WAAYK,IAE1C,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,WAAYQ,IAE1C,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 132b1f7..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 8e104a3cd9689dd27f016f54917b2aa82fc769ff Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Jan 2024 10:31:49 +0000 Subject: [PATCH 087/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 47 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 53 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 50 files changed, 6208 insertions(+), 6917 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index b92367e..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-01-01T02:40:49.822Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 30656c4..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c92f5c4..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 131632e..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index e1e3539..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -458,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -526,11 +525,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index a206441..0000000 --- a/branches.md +++ /dev/null @@ -1,53 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers. -- **deno**: [Deno][deno-url] branch for use in Deno. -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments. - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 416a0a3..96bbe7c 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..3fafdea --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function a(e,t){return s(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(n("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(n("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(n("invalid argument. Options argument must be an object. Value: `%s`.",t))}function u(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var n=Function.bind.apply(t,i);return new n}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})})),i}var d=u(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function c(e,t,i,n,r){var s,o,l,m,a,u,c;if(a=e.length,d("Collection length: %d",a),0===a)return d("Finished processing a collection."),r(null,t);for(m=a1){if(l=a(o,e))throw l;m=s}else m=e;if(!t(m))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),u;function u(e,r,s){if(!i(e))throw new TypeError(n("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!t(s))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",s));return c(e,r,o,m,(function(e,t){if(e)return s(e);s(null,t)}))}}function f(e,t,i,n,r){if(arguments.length<5)return p(i)(e,t,n);p(i,n)(e,t,r)}e(f,"factory",p);export{f as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..0fe885a --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/string-format';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;+4BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,+DAAgE,SAAUR,EAAKK,SAG1GF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,wEAAyE,QAASR,EAAKS,QAGhH,MAjBC,IAAIF,UAAWC,EAAQ,qEAAsEP,GAkBtG,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,mEAAoE4B,IAUlG,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,sEAAuEK,IAErG,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,mEAAoEQ,IAElG,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1070595..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 9ba927e..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index d48276e..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.1.1", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.1", - "@stdlib/assert-is-plain-object": "^0.1.1", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.1.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..b62bd87 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From fa119f5a57555b5bfa0e4afd1a8cf66333d90542 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Feb 2024 04:45:15 +0000 Subject: [PATCH 088/138] Transform error messages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 79800b4..d48276e 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.1.1", "@stdlib/assert-is-positive-integer": "^0.1.0", "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/string-format": "^0.1.1", + "@stdlib/error-tools-fmtprodmsg": "^0.1.1", "@stdlib/types": "^0.2.0", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", "debug": "^2.6.9" From 32765ca074bade4fa85399bd98f192812f3cd38a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Feb 2024 09:55:38 +0000 Subject: [PATCH 089/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 96bbe7c..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( `@stdlib/fs/read-file` ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( `@stdlib/fs/read-file` ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 3fafdea..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function a(e,t){return s(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(n("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(n("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(n("invalid argument. Options argument must be an object. Value: `%s`.",t))}function u(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var n=Function.bind.apply(t,i);return new n}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})})),i}var d=u(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function c(e,t,i,n,r){var s,o,l,m,a,u,c;if(a=e.length,d("Collection length: %d",a),0===a)return d("Finished processing a collection."),r(null,t);for(m=a1){if(l=a(o,e))throw l;m=s}else m=e;if(!t(m))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),u;function u(e,r,s){if(!i(e))throw new TypeError(n("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!t(s))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",s));return c(e,r,o,m,(function(e,t){if(e)return s(e);s(null,t)}))}}function f(e,t,i,n,r){if(arguments.length<5)return p(i)(e,t,n);p(i,n)(e,t,r)}e(f,"factory",p);export{f as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 0fe885a..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/string-format';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;+4BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,+DAAgE,SAAUR,EAAKK,SAG1GF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,wEAAyE,QAASR,EAAKS,QAGhH,MAjBC,IAAIF,UAAWC,EAAQ,qEAAsEP,GAkBtG,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,mEAAoE4B,IAUlG,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,sEAAuEK,IAErG,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,mEAAoEQ,IAElG,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index b62bd87..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From f700d61c14cbbaf86335ab92fe428b394ded8ff1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Feb 2024 09:55:54 +0000 Subject: [PATCH 090/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 128 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 50 files changed, 6208 insertions(+), 6922 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 7d7b065..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-02-01T03:13:08.822Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 30656c4..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index 3acd3a9..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA corresponding to v0.11.0 - uses: styfle/cancel-workflow-action@b173b6ec0100793626c2d9e6b90435061f4fc3e5 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index c92f5c4..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 131632e..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index e1e3539..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA corresponding to v3.8.1 - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..3fafdea --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function a(e,t){return s(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(n("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(n("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(n("invalid argument. Options argument must be an object. Value: `%s`.",t))}function u(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var n=Function.bind.apply(t,i);return new n}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})})),i}var d=u(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function c(e,t,i,n,r){var s,o,l,m,a,u,c;if(a=e.length,d("Collection length: %d",a),0===a)return d("Finished processing a collection."),r(null,t);for(m=a1){if(l=a(o,e))throw l;m=s}else m=e;if(!t(m))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),u;function u(e,r,s){if(!i(e))throw new TypeError(n("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!t(s))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",s));return c(e,r,o,m,(function(e,t){if(e)return s(e);s(null,t)}))}}function f(e,t,i,n,r){if(arguments.length<5)return p(i)(e,t,n);p(i,n)(e,t,r)}e(f,"factory",p);export{f as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..0fe885a --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/string-format';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;+4BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,+DAAgE,SAAUR,EAAKK,SAG1GF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,wEAAyE,QAASR,EAAKS,QAGhH,MAjBC,IAAIF,UAAWC,EAAQ,qEAAsEP,GAkBtG,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,mEAAoE4B,IAUlG,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,sEAAuEK,IAErG,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,mEAAoEQ,IAElG,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1070595..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 9ba927e..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index d48276e..7854b16 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.1.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.1.1", - "@stdlib/assert-is-collection": "^0.1.0", - "@stdlib/assert-is-function": "^0.1.1", - "@stdlib/assert-is-plain-object": "^0.1.1", - "@stdlib/assert-is-positive-integer": "^0.1.0", - "@stdlib/constants-float64-pinf": "^0.1.1", - "@stdlib/error-tools-fmtprodmsg": "^0.1.1", - "@stdlib/types": "^0.2.0", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.1.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.1.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..68e63bd --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From dafb9e2f97770329c8686410dec30e48d538af0d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 08:39:34 +0000 Subject: [PATCH 091/138] Transform error messages --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 20f46fb..25c4985 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.0", "@stdlib/assert-is-positive-integer": "^0.2.0", "@stdlib/constants-float64-pinf": "^0.2.0", - "@stdlib/string-format": "^0.2.0", + "@stdlib/error-tools-fmtprodmsg": "^0.2.0", "@stdlib/types": "^0.3.1", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.0", "debug": "^2.6.9" From e50d5bfda2aea4e421594dfca6107d6d118c464e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 17:15:36 +0000 Subject: [PATCH 092/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 3fafdea..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.1.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.1.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.1.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.1.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.1.1-esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function a(e,t){return s(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(n("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(n("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(n("invalid argument. Options argument must be an object. Value: `%s`.",t))}function u(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var n=Function.bind.apply(t,i);return new n}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})})),i}var d=u(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function c(e,t,i,n,r){var s,o,l,m,a,u,c;if(a=e.length,d("Collection length: %d",a),0===a)return d("Finished processing a collection."),r(null,t);for(m=a1){if(l=a(o,e))throw l;m=s}else m=e;if(!t(m))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),u;function u(e,r,s){if(!i(e))throw new TypeError(n("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!t(s))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",s));return c(e,r,o,m,(function(e,t){if(e)return s(e);s(null,t)}))}}function f(e,t,i,n,r){if(arguments.length<5)return p(i)(e,t,n);p(i,n)(e,t,r)}e(f,"factory",p);export{f as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 0fe885a..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/string-format';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;+4BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,+DAAgE,SAAUR,EAAKK,SAG1GF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,wEAAyE,QAASR,EAAKS,QAGhH,MAjBC,IAAIF,UAAWC,EAAQ,qEAAsEP,GAkBtG,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,mEAAoE4B,IAUlG,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,sEAAuEK,IAErG,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,mEAAoEQ,IAElG,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 68e63bd..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From d672a256fd65ee2adb08a1dcc893529d9d92f6e1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 17:16:01 +0000 Subject: [PATCH 093/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 --- .github/workflows/publish.yml | 255 - .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 6177 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 49 files changed, 6208 insertions(+), 6925 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index 62f2657..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA corresponding to v3.1.3 - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9106b5d..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a542392 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.0-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.0-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.0-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.0-esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function a(e,t){return s(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(n("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(n("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(n("invalid argument. Options argument must be an object. Value: `%s`.",t))}function u(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var n=Function.bind.apply(t,i);return new n}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})})),i}var d=u(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function c(e,t,i,n,r){var s,o,l,m,a,u,c;if(a=e.length,d("Collection length: %d",a),0===a)return d("Finished processing a collection."),r(null,t);for(m=a1){if(l=a(o,e))throw l;m=s}else m=e;if(!t(m))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),u;function u(e,r,s){if(!i(e))throw new TypeError(n("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!t(s))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",s));return c(e,r,o,m,(function(e,t){if(e)return s(e);s(null,t)}))}}function f(e,t,i,n,r){if(arguments.length<5)return p(i)(e,t,n);p(i,n)(e,t,r)}e(f,"factory",p);export{f as default,p as factory}; +//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map new file mode 100644 index 0000000..0fe885a --- /dev/null +++ b/index.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/string-format';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;+4BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,+DAAgE,SAAUR,EAAKK,SAG1GF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,wEAAyE,QAASR,EAAKS,QAGhH,MAjBC,IAAIF,UAAWC,EAAQ,qEAAsEP,GAkBtG,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,mEAAoE4B,IAUlG,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,sEAAuEK,IAErG,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,mEAAoEQ,IAElG,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1070595..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 9ba927e..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 25c4985..6887d8b 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.0", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.1.1", - "@stdlib/assert-is-boolean": "^0.2.0", - "@stdlib/assert-is-collection": "^0.2.0", - "@stdlib/assert-is-function": "^0.2.0", - "@stdlib/assert-is-plain-object": "^0.2.0", - "@stdlib/assert-is-positive-integer": "^0.2.0", - "@stdlib/constants-float64-pinf": "^0.2.0", - "@stdlib/error-tools-fmtprodmsg": "^0.2.0", - "@stdlib/types": "^0.3.1", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.0", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.1.1", - "@stdlib/fs-read-file": "^0.1.1", - "@stdlib/utils-noop": "^0.2.0", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.1.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..02bc090 --- /dev/null +++ b/stats.html @@ -0,0 +1,6177 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From d2a0f760c0036d04a14585a0c6e0ae54715bb608 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 20:29:49 +0000 Subject: [PATCH 094/138] Update README.md for ESM bundle v0.2.0 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d4f17b0..c979a18 100644 --- a/README.md +++ b/README.md @@ -52,13 +52,13 @@ limitations under the License. ## Usage ```javascript -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.0-esm/index.mjs'; ``` You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.0-esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +381,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.0-esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From d7d0ebd8269867afb6a0e7f0fa9370ad1f98aee8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Wed, 14 Feb 2024 20:29:50 +0000 Subject: [PATCH 095/138] Auto-generated commit --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c979a18..5c1dd61 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/utils-async-reduce/tags). For example, + ```javascript import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.0-esm/index.mjs'; ``` @@ -58,7 +63,7 @@ import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduc You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.0-esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +386,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.0-esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From 0ea6a9bf6970dcb52f3b9288cf8bfb4838d80283 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 24 Feb 2024 16:08:31 +0000 Subject: [PATCH 096/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 55fee2c..980e4c7 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.1", "@stdlib/assert-is-positive-integer": "^0.2.1", "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", "debug": "^2.6.9" From c420f73e0dcec7dc31f8df24f80ec77ef86b8fd2 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 24 Feb 2024 17:55:23 +0000 Subject: [PATCH 097/138] Remove files --- index.d.ts | 406 ---- index.mjs | 4 - index.mjs.map | 1 - stats.html | 6177 ------------------------------------------------- 4 files changed, 6588 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a542392..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.1.0-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.0-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.1.0-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/string-format@v0.1.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.0-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.0-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.0-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.0-esm/index.mjs";import{isPrimitive as m}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.1.0-esm/index.mjs";function a(e,t){return s(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(n("invalid option. `%s` option must be a boolean. Option: `%s`.","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!m(e.limit))?new TypeError(n("invalid option. `%s` option must be a positive integer. Option: `%s`.","limit",e.limit)):null):new TypeError(n("invalid argument. Options argument must be an object. Value: `%s`.",t))}function u(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){if(this instanceof e){var i=[null];i.push.apply(i,arguments);var n=Function.bind.apply(t,i);return new n}return t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})})),i}var d=u(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function c(e,t,i,n,r){var s,o,l,m,a,u,c;if(a=e.length,d("Collection length: %d",a),0===a)return d("Finished processing a collection."),r(null,t);for(m=a1){if(l=a(o,e))throw l;m=s}else m=e;if(!t(m))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",m));return void 0===o.series&&void 0===o.limit&&(o.series=!0),o.series?o.limit=1:o.limit||(o.limit=r),u;function u(e,r,s){if(!i(e))throw new TypeError(n("invalid argument. First argument must be a collection. Value: `%s`.",e));if(!t(s))throw new TypeError(n("invalid argument. Last argument must be a function. Value: `%s`.",s));return c(e,r,o,m,(function(e,t){if(e)return s(e);s(null,t)}))}}function f(e,t,i,n,r){if(arguments.length<5)return p(i)(e,t,n);p(i,n)(e,t,r)}e(f,"factory",p);export{f as default,p as factory}; -//# sourceMappingURL=index.mjs.map diff --git a/index.mjs.map b/index.mjs.map deleted file mode 100644 index 0fe885a..0000000 --- a/index.mjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"index.mjs","sources":["../lib/validate.js","../lib/limit.js","../lib/factory.js","../lib/main.js","../lib/index.js"],"sourcesContent":["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/string-format';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default limit;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isFunction from '@stdlib/assert-is-function';\nimport isCollection from '@stdlib/assert-is-collection';\nimport format from '@stdlib/string-format';\nimport PINF from '@stdlib/constants-float64-pinf';\nimport validate from './validate.js';\nimport limit from './limit.js';\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["validate","opts","options","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","debug","logger","collection","acc","fcn","done","maxIndex","count","flg","lim","len","idx","i","length","next","cb","error","result","clbk","JSON","stringify","call","message","factory","err","f","arguments","isFunction","PINF","reduceAsync","initial","isCollection","setReadOnly","main"],"mappings":";;+4BAsDA,SAASA,EAAUC,EAAMC,GACxB,OAAMC,EAAUD,IAGXE,EAAYF,EAAS,aACzBD,EAAKI,QAAUH,EAAQG,SAEnBD,EAAYF,EAAS,YACzBD,EAAKK,OAASJ,EAAQI,QAChBC,EAAWN,EAAKK,SACd,IAAIE,UAAWC,EAAQ,+DAAgE,SAAUR,EAAKK,SAG1GF,EAAYF,EAAS,WACzBD,EAAKS,MAAQR,EAAQQ,OACfC,EAAmBV,EAAKS,QACtB,IAAIF,UAAWC,EAAQ,wEAAyE,QAASR,EAAKS,QAGhH,MAjBC,IAAIF,UAAWC,EAAQ,qEAAsEP,GAkBtG,8eC/CIU,uDAAQC,CAAQ,sBAkBpB,SAASH,EAAOI,EAAYC,EAAKd,EAAMe,EAAKC,GAC3C,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMR,EAAWW,OACjBb,EAAO,wBAAyBU,GAEnB,IAARA,EAEJ,OADAV,EAAO,qCACAK,EAAM,KAAMF,GAapB,IAVCM,EADIC,EAAMrB,EAAKS,MACTY,EAEArB,EAAKS,MAEZE,EAAO,wBAAyBS,GAChCT,EAAO,0BAA2BI,EAAIS,QAEtCP,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVQ,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKT,EAAL,CAIA,GAAKQ,EAEJ,OADAR,GAAM,EACCU,EAAMF,GAEdhB,EAAO,kBAAmBmB,KAAKC,UAAWH,IAC1Cd,EAAMc,EACNC,GAPC,CAQD,CA5BDlB,EAAO,6BADPW,GAAO,EACmCQ,KAAKC,UAAWlB,EAAYS,KAClD,IAAfP,EAAIS,OACRT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOI,GACtB,IAAfX,EAAIS,OACfT,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKI,GAErDX,EAAIiB,KAAMhC,EAAKI,QAASU,EAAKD,EAAYS,GAAOA,EAAKT,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJhB,EAAO,2BAA4BgB,EAAMM,SAClCjB,EAAMW,KAGdhB,EAAO,0CADPO,GAAS,EACgDG,GACpDC,EAAML,EACHQ,IAEHP,IAAUG,GACdV,EAAO,qCACAK,EAAM,KAAMF,SAFpB,EAIA,CACF,CC3CA,SAASoB,EAASjC,EAASc,GAC1B,IAAIf,EACAmC,EACAC,EAGJ,GADApC,EAAO,CAAA,EACFqC,UAAUb,OAAS,EAAI,CAE3B,GADAW,EAAMpC,EAAUC,EAAMC,GAErB,MAAMkC,EAEPC,EAAIrB,CACN,MACEqB,EAAInC,EAEL,IAAMqC,EAAYF,GACjB,MAAM,IAAI7B,UAAWC,EAAQ,mEAAoE4B,IAUlG,YARqB,IAAhBpC,EAAKK,aAAoC,IAAfL,EAAKS,QACnCT,EAAKK,QAAS,GAEVL,EAAKK,OACTL,EAAKS,MAAQ,EACDT,EAAKS,QACjBT,EAAKS,MAAQ8B,GAEPC,EAaP,SAASA,EAAa3B,EAAY4B,EAASzB,GAC1C,IAAM0B,EAAc7B,GACnB,MAAM,IAAIN,UAAWC,EAAQ,sEAAuEK,IAErG,IAAMyB,EAAYtB,GACjB,MAAM,IAAIT,UAAWC,EAAQ,mEAAoEQ,IAElG,OAAOP,EAAOI,EAAY4B,EAASzC,EAAMoC,GAUzC,SAAeT,EAAOb,GACrB,GAAKa,EACJ,OAAOX,EAAMW,GAEdX,EAAM,KAAMF,EACZ,GACD,CACF,CC1EA,SAAS0B,EAAa3B,EAAY4B,EAASxC,EAASc,EAAKC,GACxD,GAAKqB,UAAUb,OAAS,EACvB,OAAOU,EAASjC,EAATiC,CAAoBrB,EAAY4B,EAAS1B,GAEjDmB,EAASjC,EAASc,EAAlBmB,CAAyBrB,EAAY4B,EAASzB,EAC/C,CClBA2B,EAAAC,EAAA,UAAAV"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 02bc090..0000000 --- a/stats.html +++ /dev/null @@ -1,6177 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From b0b026b121e300bed58a617fa98a9395201d895a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 24 Feb 2024 17:55:41 +0000 Subject: [PATCH 098/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 228 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 64 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 49 files changed, 4873 insertions(+), 6919 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9106b5d..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..a259c5f --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 980e4c7..f48327e 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.1", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,43 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.1", - "@stdlib/assert-is-boolean": "^0.2.1", - "@stdlib/assert-is-collection": "^0.2.1", - "@stdlib/assert-is-function": "^0.2.1", - "@stdlib/assert-is-plain-object": "^0.2.1", - "@stdlib/assert-is-positive-integer": "^0.2.1", - "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", - "debug": "^2.6.9" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.1", - "@stdlib/fs-read-file": "^0.2.1", - "@stdlib/utils-noop": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.0" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..1781b91 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 0d4a90673316b9c15f7d3520b14da38fcedab7bc Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 24 Feb 2024 18:47:57 +0000 Subject: [PATCH 099/138] Update README.md for ESM bundle v0.2.1 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3fe8c04..7a90530 100644 --- a/README.md +++ b/README.md @@ -52,13 +52,13 @@ limitations under the License. ## Usage ```javascript -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.1-esm/index.mjs'; ``` You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.1-esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +381,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.1-esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From b06b0a37a09d24ed93ba6887073e2544985b85d4 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 24 Feb 2024 18:47:57 +0000 Subject: [PATCH 100/138] Auto-generated commit --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7a90530..0769794 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/utils-async-reduce/tags). For example, + ```javascript import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.1-esm/index.mjs'; ``` @@ -58,7 +63,7 @@ import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduc You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.1-esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +386,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.1-esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From b8641d9437378dc15d08774b8e04c3df157bfd4d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Mar 2024 04:21:10 +0000 Subject: [PATCH 101/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 330fc03..11eb91c 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.1", "@stdlib/assert-is-positive-integer": "^0.2.1", "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", "debug": "^2.6.9", From 30033cbc2fc2401cbbe83f2c5d928d2e5c0b14a5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Mar 2024 08:31:01 +0000 Subject: [PATCH 102/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index a259c5f..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.0-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 1781b91..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 0d2b734d76e5f1991c625c198bd0675e019e324c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Mar 2024 08:31:13 +0000 Subject: [PATCH 103/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 229 - .npmrc | 28 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 50 files changed, 4873 insertions(+), 6922 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index f85155a..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-03-01T03:09:28.205Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index 9106b5d..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA corresponding to v2.0.0 - uses: act10ns/slack@ed1309ab9862e57e9e583e51c7889486b9a00b0f - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..ede8e47 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 11eb91c..f48327e 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.1", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.1", - "@stdlib/assert-is-boolean": "^0.2.1", - "@stdlib/assert-is-collection": "^0.2.1", - "@stdlib/assert-is-function": "^0.2.1", - "@stdlib/assert-is-plain-object": "^0.2.1", - "@stdlib/assert-is-positive-integer": "^0.2.1", - "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.1", - "@stdlib/fs-read-file": "^0.2.1", - "@stdlib/utils-noop": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..566e6fe --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From a0b63776ddd3bfabb44eaf38c13c61905ced1a52 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Apr 2024 03:51:15 +0000 Subject: [PATCH 104/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 330fc03..11eb91c 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.1", "@stdlib/assert-is-positive-integer": "^0.2.1", "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", "debug": "^2.6.9", From 5a4b8121b0b000022472ba982e804727e4205b21 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Apr 2024 08:17:44 +0000 Subject: [PATCH 105/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index ede8e47..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 566e6fe..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 02dc5d98bfd55f255d081b9c52848795f712c416 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 1 Apr 2024 08:17:54 +0000 Subject: [PATCH 106/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 132 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 50 files changed, 4873 insertions(+), 6925 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index d01820b..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-04-01T02:35:33.885Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index ec90164..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..ede8e47 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 11eb91c..f48327e 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.1", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.1", - "@stdlib/assert-is-boolean": "^0.2.1", - "@stdlib/assert-is-collection": "^0.2.1", - "@stdlib/assert-is-function": "^0.2.1", - "@stdlib/assert-is-plain-object": "^0.2.1", - "@stdlib/assert-is-positive-integer": "^0.2.1", - "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.1", - "@stdlib/fs-read-file": "^0.2.1", - "@stdlib/utils-noop": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..566e6fe --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 49280752f9441dde4d56711254f0ec904cda38b1 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 12 Apr 2024 00:01:10 +0000 Subject: [PATCH 107/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 330fc03..11eb91c 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.1", "@stdlib/assert-is-positive-integer": "^0.2.1", "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/string-format": "^0.2.1", + "@stdlib/error-tools-fmtprodmsg": "^0.2.1", "@stdlib/types": "^0.3.2", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", "debug": "^2.6.9", From dc26b8fe8a65aad14235f6084df793ac141174a8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 12 Apr 2024 05:03:16 +0000 Subject: [PATCH 108/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index ede8e47..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 566e6fe..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 2322dc6683f96c12fc05ccf81668f33a78f0572a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 12 Apr 2024 05:03:31 +0000 Subject: [PATCH 109/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 49 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 797 ---- .github/workflows/publish.yml | 249 -- .github/workflows/test.yml | 100 - .github/workflows/test_bundles.yml | 189 - .github/workflows/test_coverage.yml | 134 - .github/workflows/test_install.yml | 86 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 5 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 49 files changed, 4873 insertions(+), 6926 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 10a16e6..0000000 --- a/.gitattributes +++ /dev/null @@ -1,49 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/deps/** linguist-vendored=false -/lib/node_modules/** linguist-vendored=false linguist-generated=false -test/fixtures/** linguist-vendored=false -tools/** linguist-vendored=false - -# Override what is considered "documentation" by GitHub's linguist: -examples/** linguist-documentation=false diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index ec90164..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,797 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - steps: ${{ toJson(steps) }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure git: - - name: 'Configure git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..ede8e47 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 11eb91c..f48327e 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.1", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.1", - "@stdlib/assert-is-boolean": "^0.2.1", - "@stdlib/assert-is-collection": "^0.2.1", - "@stdlib/assert-is-function": "^0.2.1", - "@stdlib/assert-is-plain-object": "^0.2.1", - "@stdlib/assert-is-positive-integer": "^0.2.1", - "@stdlib/constants-float64-pinf": "^0.2.1", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.1" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.1", - "@stdlib/fs-read-file": "^0.2.1", - "@stdlib/utils-noop": "^0.2.1", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..566e6fe --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From d1ce08dea8cbc3c1dab240a6456de96a91459df5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 28 Jul 2024 00:13:59 +0000 Subject: [PATCH 110/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 8c44bdd..faf51f4 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.3.2", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From bcf93d9a9cc75a70fa858ec38afeec25c6f6d5d8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 28 Jul 2024 00:23:14 +0000 Subject: [PATCH 111/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index ede8e47..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.1-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.1-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.1-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.1-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.1-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.1-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.1-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.1-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 566e6fe..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From edd37e9961354b29adcbe709b9deaddf268effda Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 28 Jul 2024 00:23:31 +0000 Subject: [PATCH 112/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 188 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 212 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 49 files changed, 4873 insertions(+), 7144 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index faf51f4..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.1", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.1" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From f82143679b90def0c78e2bfac5a6bd1e8beddb30 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 28 Jul 2024 00:25:07 +0000 Subject: [PATCH 113/138] Update README.md for ESM bundle v0.2.2 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bdd77e2..8f08bb8 100644 --- a/README.md +++ b/README.md @@ -52,13 +52,13 @@ limitations under the License. ## Usage ```javascript -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.2-esm/index.mjs'; ``` You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.2-esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +381,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.2-esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From dd7e48373cb167177df656f8f497bc624095e95c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 28 Jul 2024 00:25:08 +0000 Subject: [PATCH 114/138] Auto-generated commit --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8f08bb8..be20372 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ limitations under the License. ## Usage +```javascript +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; +``` +The previous example will load the latest bundled code from the esm branch. Alternatively, you may load a specific version by loading the file from one of the [tagged bundles](https://github.com/stdlib-js/utils-async-reduce/tags). For example, + ```javascript import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.2-esm/index.mjs'; ``` @@ -58,7 +63,7 @@ import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduc You can also import the following named exports from the package: ```javascript -import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.2-esm/index.mjs'; +import { factory } from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; ``` #### reduceAsync( collection, initial, \[options,] reducer, done ) @@ -381,7 +386,7 @@ The function accepts the same `options` as `reduceAsync()`. var resolve = require( 'path' ).resolve; import readFile from 'https://cdn.jsdelivr.net/gh/stdlib-js/fs-read-file@esm/index.mjs'; -import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@v0.2.2-esm/index.mjs'; +import reduceAsync from 'https://cdn.jsdelivr.net/gh/stdlib-js/utils-async-reduce@esm/index.mjs'; var files = [ resolve( __dirname, 'package.json' ), From 39da4ad02d8342a859f776ae2cf770d8acf2822c Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Aug 2024 04:24:18 +0000 Subject: [PATCH 115/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 69442fd..e86e409 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.3.2", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From 0725bbd9ad35b1e73b21beae2211b55e4a3e9aff Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Aug 2024 08:18:35 +0000 Subject: [PATCH 116/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From d8f36c30ff651348e2c1ada7584a9417fa73dff6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 1 Aug 2024 08:18:46 +0000 Subject: [PATCH 117/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 158 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 50 files changed, 4873 insertions(+), 7093 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index a33d638..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-08-01T03:09:17.447Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index e86e409..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From f5ab05158c87e089a079537cab5f088e7463a76e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 3 Aug 2024 19:12:54 +0000 Subject: [PATCH 118/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index 69442fd..e86e409 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.3.2", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From c997e335e01d92fc1be11acdb8aac9060f08df5d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 3 Aug 2024 22:16:19 +0000 Subject: [PATCH 119/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 07e23c68d81b1443d4565b3aa5e03f6aeaaa2287 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 3 Aug 2024 22:16:38 +0000 Subject: [PATCH 120/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 212 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 50 files changed, 4873 insertions(+), 7147 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 2285ef7..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-08-03T18:16:41.251Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index e86e409..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.3.2", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From a1d7994cf551213f6956d53c994ba8c415e0bfbb Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Sep 2024 04:31:24 +0000 Subject: [PATCH 121/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index f3bad36..ead46ad 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.1", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From f9465af275c35ac1c371fda53a0cd1cee448b37a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Sep 2024 08:09:10 +0000 Subject: [PATCH 122/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From e6d25367725575d3b30f05cf6b6c61eae6fa886e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Sep 2024 08:09:24 +0000 Subject: [PATCH 123/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 211 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 50 files changed, 4873 insertions(+), 7146 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index f0077ba..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-09-01T03:21:02.791Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index df998c8..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - `accumulator`: accumulated value - - `value`: collection value - - `index`: collection index - - `collection`: the input collection - - `next`: a callback to be invoked after processing a collection `value` - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `next` - - If `reducer` accepts four arguments, `reducer` is provided: - - - `accumulator` - - `value` - - `index` - - `next` - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - `error`: error argument - - `accumulator`: accumulated value - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index ead46ad..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.1", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 057ad010970a9159c38b10c60e6d8dc31313394d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 21 Sep 2024 21:52:14 +0000 Subject: [PATCH 124/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index f3bad36..ead46ad 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.1", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From 981933d7cd79f4e69eb3a381a60cd57484852ac7 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 21 Sep 2024 22:17:53 +0000 Subject: [PATCH 125/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From e4154a2f937ab651fa2c75e2ab94d6762a4ad652 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sat, 21 Sep 2024 22:18:06 +0000 Subject: [PATCH 126/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 245 -- CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 49 files changed, 4873 insertions(+), 7179 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index 3d38a61..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - accumulator: accumulated value. - - value: collection value. - - index: collection index. - - collection: the input collection. - - next: a callback to be invoked after processing a collection `value`. - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - accumulator - - value - - next - - If `reducer` accepts four arguments, `reducer` is provided: - - - accumulator - - value - - index - - next - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - error: error argument. - - accumulator: accumulated value. - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index ead46ad..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.1", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From ace23efe545bfa38f2cd8f0a185f2bdf040743e8 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Oct 2024 04:40:05 +0000 Subject: [PATCH 127/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index f3bad36..ead46ad 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.1", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From 7d8415e7e9c7af954ee8abf0ea7f1f4c3370da49 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Oct 2024 08:22:24 +0000 Subject: [PATCH 128/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 0f3039b160967113bfcb2de81812c58d11e54cbd Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 1 Oct 2024 08:22:39 +0000 Subject: [PATCH 129/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 245 -- CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 50 files changed, 4873 insertions(+), 7180 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 1c64f63..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-10-01T03:30:43.554Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index 3d38a61..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - accumulator: accumulated value. - - value: collection value. - - index: collection index. - - collection: the input collection. - - next: a callback to be invoked after processing a collection `value`. - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - accumulator - - value - - next - - If `reducer` accepts four arguments, `reducer` is provided: - - - accumulator - - value - - index - - next - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - error: error argument. - - accumulator: accumulated value. - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index ead46ad..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.1", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From bbaa588e24f239c09df7a30e1a5fc2b7f05137e6 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Nov 2024 04:37:37 +0000 Subject: [PATCH 130/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index f3bad36..ead46ad 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.1", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From ffee3fbf2f18220f681e2ad7cd30224f5b2882d5 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Nov 2024 07:55:08 +0000 Subject: [PATCH 131/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From 9d18e6fad18e9357564f53858ecdf2319bbd68c0 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Fri, 1 Nov 2024 07:55:21 +0000 Subject: [PATCH 132/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 ---- .github/workflows/publish.yml | 252 -- .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 245 -- CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 --- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 +++++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ----- test/test.js | 38 - test/test.main.js | 1020 ----- test/test.validate.js | 181 - 50 files changed, 4873 insertions(+), 7180 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 5c821f7..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-11-01T03:33:23.646Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index 3d38a61..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - accumulator: accumulated value. - - value: collection value. - - index: collection index. - - collection: the input collection. - - next: a callback to be invoked after processing a collection `value`. - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - accumulator - - value - - next - - If `reducer` accepts four arguments, `reducer` is provided: - - - accumulator - - value - - index - - next - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - error: error argument. - - accumulator: accumulated value. - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index ead46ad..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.1", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From 0ae860253d91666fa3e9fc1f0afdeb6f3a9d5b2f Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Dec 2024 04:51:49 +0000 Subject: [PATCH 133/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index d19ae29..8b43a45 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.3", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From 288bb01a7cc46940b2fcb5b75892dc18a71cb9ae Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Dec 2024 08:29:20 +0000 Subject: [PATCH 134/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From da6ab768251dc7b1a32cd521c25e4c89b576d506 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 1 Dec 2024 08:29:41 +0000 Subject: [PATCH 135/138] Auto-generated commit --- .editorconfig | 181 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 --- .github/workflows/publish.yml | 252 - .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .github/workflows/test_published_package.yml | 105 - .gitignore | 190 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 245 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 ++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 51 files changed, 4873 insertions(+), 7285 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .github/workflows/test_published_package.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 60d743f..0000000 --- a/.editorconfig +++ /dev/null @@ -1,181 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 -insert_final_newline = false - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = false - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 6aa0b41..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2024-12-01T03:38:12.215Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 694e99a..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index 3d38a61..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - accumulator: accumulated value. - - value: collection value. - - index: collection index. - - collection: the input collection. - - next: a callback to be invoked after processing a collection `value`. - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - accumulator - - value - - next - - If `reducer` accepts four arguments, `reducer` is provided: - - - accumulator - - value - - index - - next - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - error: error argument. - - accumulator: accumulated value. - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..8799e08 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 8b43a45..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.3", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -}); From d380e28171f04e0553522e026c3960e00a1c9f89 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 17 Mar 2025 01:23:42 +0000 Subject: [PATCH 136/138] Transform error messages --- lib/factory.js | 8 ++++---- lib/validate.js | 8 ++++---- package.json | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/factory.js b/lib/factory.js index 1070595..1b5acc5 100644 --- a/lib/factory.js +++ b/lib/factory.js @@ -22,7 +22,7 @@ var isFunction = require( '@stdlib/assert-is-function' ); var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); var PINF = require( '@stdlib/constants-float64-pinf' ); var validate = require( './validate.js' ); var limit = require( './limit.js' ); @@ -109,7 +109,7 @@ function factory( options, fcn ) { f = options; } if ( !isFunction( f ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) ); + throw new TypeError( format( '1Rb3q', f ) ); } if ( opts.series === void 0 && opts.limit === void 0 ) { opts.series = true; @@ -134,10 +134,10 @@ function factory( options, fcn ) { */ function reduceAsync( collection, initial, done ) { if ( !isCollection( collection ) ) { - throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); + throw new TypeError( format( '1RbAh', collection ) ); } if ( !isFunction( done ) ) { - throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); + throw new TypeError( format( '1Rb3q', done ) ); } return limit( collection, initial, opts, f, clbk ); diff --git a/lib/validate.js b/lib/validate.js index 9ba927e..7f6ee29 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -24,7 +24,7 @@ var isObject = require( '@stdlib/assert-is-plain-object' ); var hasOwnProp = require( '@stdlib/assert-has-own-property' ); var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/string-format' ); +var format = require( '@stdlib/error-tools-fmtprodmsg' ); // MAIN // @@ -54,7 +54,7 @@ var format = require( '@stdlib/string-format' ); */ function validate( opts, options ) { if ( !isObject( options ) ) { - return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + return new TypeError( format( '1Rb2V', options ) ); } if ( hasOwnProp( options, 'thisArg' ) ) { opts.thisArg = options.thisArg; @@ -62,13 +62,13 @@ function validate( opts, options ) { if ( hasOwnProp( options, 'series' ) ) { opts.series = options.series; if ( !isBoolean( opts.series ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) ); + return new TypeError( format( '1Rb2o', 'series', opts.series ) ); } } if ( hasOwnProp( options, 'limit' ) ) { opts.limit = options.limit; if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) ); + return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); } } return null; diff --git a/package.json b/package.json index d19ae29..8b43a45 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "@stdlib/assert-is-plain-object": "^0.2.2", "@stdlib/assert-is-positive-integer": "^0.2.2", "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/string-format": "^0.2.2", + "@stdlib/error-tools-fmtprodmsg": "^0.2.2", "@stdlib/types": "^0.4.3", "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", "debug": "^2.6.9", From 76e6c8adef454134b420cf170d011f09f30988de Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 17 Mar 2025 01:47:34 +0000 Subject: [PATCH 137/138] Remove files --- index.d.ts | 406 ----- index.mjs | 4 - index.mjs.map | 1 - stats.html | 4842 ------------------------------------------------- 4 files changed, 5253 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.mjs delete mode 100644 index.mjs.map delete mode 100644 stats.html diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b296f97..0000000 --- a/index.d.ts +++ /dev/null @@ -1,406 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -// TypeScript Version: 4.1 - -/// - -import { Collection } from '@stdlib/types/array'; - -/** -* Interface defining function options. -*/ -interface Options { - /** - * Execution context. - */ - thisArg?: ThisParameterType>; - - /** - * The maximum number of pending invocations at any one time. - */ - limit?: number; - - /** - * Boolean indicating whether to sequentially invoke the reducer function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: true. - */ - series?: boolean; -} - -/** -* Callback function. -*/ -type Nullary = () => void; - -/** -* Callback function. -* -* @param error - encountered error or null -*/ -type Unary = ( error: Error | null ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Binary = ( error: Error | null, accumulator: U ) => void; - -/** -* Callback function. -* -* @param error - encountered error or null -* @param accumulator - accumulated value -*/ -type Callback = Nullary | Unary | Binary; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param next - a callback to be invoked after processing a collection `value` -*/ -type TernaryReducer = ( this: V, accumulator: U, value: T, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuaternaryReducer = ( this: V, accumulator: U, value: T, index: number, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type QuinaryReducer = ( this: V, accumulator: U, value: T, index: number, collection: Collection, next: Callback ) => void; - -/** -* Reducer function invoked for each element in a collection. -* -* @param accumulator - accumulated value -* @param value - collection value -* @param index - collection index -* @param collection - the input collection -* @param next - a callback to be invoked after processing a collection `value` -*/ -type Reducer = TernaryReducer | QuaternaryReducer | QuinaryReducer; - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* @param collection - input collection -* @param initial - initial value -* @param done - function to invoke upon completion -*/ -type FactoryFunction = ( collection: Collection, initial: U, done: Callback ) => void; - -/** -* Interface for `reduceAsync`. -*/ -interface ReduceAsync { - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * @throws must provide valid options - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, {}, read, done ); - */ - ( collection: Collection, initial: U, options: Options, reducer: Reducer, done: Callback ): void; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param collection - input collection - * @param initial - initial value - * @param reducer - function to invoke for each element in a collection - * @param done - function to invoke upon completion - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, read, done ); - */ - ( collection: Collection, initial: U, reducer: Reducer, done: Callback ): void; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param options - function options - * @param options.thisArg - execution context - * @param options.limit - maximum number of pending invocations at any one time - * @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * var opts = { - * 'series': false - * }; - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( opts, read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( options: Options, reducer: Reducer ): FactoryFunction; - - /** - * Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. - * - * ## Notes - * - * - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. - * - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). - * - * @param reducer - function to invoke for each element in a collection - * @throws must provide valid options - * @returns function which invokes the provided function once for each element in a collection - * - * @example - * var readFile = require( '@stdlib/fs-read-file' ); - * - * function read( acc, file, next ) { - * var opts = { - * 'encoding': 'utf8' - * }; - * readFile( file, opts, onFile ); - * - * function onFile( error, data ) { - * if ( error ) { - * return next( null, acc ); - * } - * acc.count += 1; - * next( null, acc ); - * } - * } - * - * // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: - * var reduceAsync = factory( read ); - * - * // Create a collection over which to iterate: - * var files = [ - * './beep.js', - * './boop.js' - * ]; - * - * // Define a callback which handles errors: - * function done( error, acc ) { - * if ( error ) { - * throw error; - * } - * console.log( acc.count ); - * } - * - * // Run `read` for each element in `files`: - * var acc = { - * 'count': 0 - * }; - * reduceAsync( files, acc, done ); - */ - factory( reducer: Reducer ): FactoryFunction; -} - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param collection - input collection -* @param initial - initial value -* @param options - function options -* @param options.thisArg - execution context -* @param options.limit - maximum number of pending invocations at any one time -* @param options.series - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection (default: true) -* @param reducer - function to invoke for each element in a collection -* @param done - function to invoke upon completion -* @throws must provide valid options -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -declare var reduceAsync: ReduceAsync; - - -// EXPORTS // - -export = reduceAsync; diff --git a/index.mjs b/index.mjs deleted file mode 100644 index 8799e08..0000000 --- a/index.mjs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) 2024 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 -/// -import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/stats.html b/stats.html deleted file mode 100644 index 6d3e789..0000000 --- a/stats.html +++ /dev/null @@ -1,4842 +0,0 @@ - - - - - - - - Rollup Visualizer - - - -
- - - - - From f9169653d8c92641fa012b1412812abbe467422d Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Mon, 17 Mar 2025 01:47:46 +0000 Subject: [PATCH 138/138] Auto-generated commit --- .editorconfig | 180 - .eslintrc.js | 1 - .gitattributes | 66 - .github/.keepalive | 1 - .github/PULL_REQUEST_TEMPLATE.md | 7 - .github/workflows/benchmark.yml | 64 - .github/workflows/cancel.yml | 57 - .github/workflows/close_pull_requests.yml | 54 - .github/workflows/examples.yml | 64 - .github/workflows/npm_downloads.yml | 112 - .github/workflows/productionize.yml | 794 --- .github/workflows/publish.yml | 252 - .github/workflows/test.yml | 99 - .github/workflows/test_bundles.yml | 186 - .github/workflows/test_coverage.yml | 133 - .github/workflows/test_install.yml | 85 - .github/workflows/test_published_package.yml | 105 - .gitignore | 194 - .npmignore | 229 - .npmrc | 31 - CHANGELOG.md | 245 - CITATION.cff | 30 - CODE_OF_CONDUCT.md | 3 - CONTRIBUTING.md | 3 - Makefile | 534 -- README.md | 49 +- SECURITY.md | 5 - benchmark/benchmark.factory.js | 73 - benchmark/benchmark.js | 160 - branches.md | 56 - dist/index.d.ts | 3 - dist/index.js | 11 - dist/index.js.map | 7 - docs/repl.txt | 223 - docs/types/test.ts | 164 - examples/index.js | 55 - docs/types/index.d.ts => index.d.ts | 2 +- index.mjs | 4 + index.mjs.map | 1 + lib/factory.js | 164 - lib/index.js | 77 - lib/limit.js | 144 - lib/main.js | 95 - lib/validate.js | 80 - package.json | 65 +- stats.html | 4842 ++++++++++++++++++ test/dist/test.js | 33 - test/test.factory.js | 1080 ---- test/test.js | 38 - test/test.main.js | 1020 ---- test/test.validate.js | 181 - 51 files changed, 4873 insertions(+), 7288 deletions(-) delete mode 100644 .editorconfig delete mode 100644 .eslintrc.js delete mode 100644 .gitattributes delete mode 100644 .github/.keepalive delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md delete mode 100644 .github/workflows/benchmark.yml delete mode 100644 .github/workflows/cancel.yml delete mode 100644 .github/workflows/close_pull_requests.yml delete mode 100644 .github/workflows/examples.yml delete mode 100644 .github/workflows/npm_downloads.yml delete mode 100644 .github/workflows/productionize.yml delete mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/test.yml delete mode 100644 .github/workflows/test_bundles.yml delete mode 100644 .github/workflows/test_coverage.yml delete mode 100644 .github/workflows/test_install.yml delete mode 100644 .github/workflows/test_published_package.yml delete mode 100644 .gitignore delete mode 100644 .npmignore delete mode 100644 .npmrc delete mode 100644 CHANGELOG.md delete mode 100644 CITATION.cff delete mode 100644 CODE_OF_CONDUCT.md delete mode 100644 CONTRIBUTING.md delete mode 100644 Makefile delete mode 100644 SECURITY.md delete mode 100644 benchmark/benchmark.factory.js delete mode 100644 benchmark/benchmark.js delete mode 100644 branches.md delete mode 100644 dist/index.d.ts delete mode 100644 dist/index.js delete mode 100644 dist/index.js.map delete mode 100644 docs/repl.txt delete mode 100644 docs/types/test.ts delete mode 100644 examples/index.js rename docs/types/index.d.ts => index.d.ts (99%) create mode 100644 index.mjs create mode 100644 index.mjs.map delete mode 100644 lib/factory.js delete mode 100644 lib/index.js delete mode 100644 lib/limit.js delete mode 100644 lib/main.js delete mode 100644 lib/validate.js create mode 100644 stats.html delete mode 100644 test/dist/test.js delete mode 100644 test/test.factory.js delete mode 100644 test/test.js delete mode 100644 test/test.main.js delete mode 100644 test/test.validate.js diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index dab5d2a..0000000 --- a/.editorconfig +++ /dev/null @@ -1,180 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# EditorConfig configuration file (see ). - -# Indicate that this file is a root-level configuration file: -root = true - -# Set properties for all files: -[*] -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - -# Set properties for JavaScript files: -[*.{js,js.txt}] -indent_style = tab - -# Set properties for JavaScript ES module files: -[*.{mjs,mjs.txt}] -indent_style = tab - -# Set properties for JavaScript CommonJS files: -[*.{cjs,cjs.txt}] -indent_style = tab - -# Set properties for JSON files: -[*.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `cli_opts.json` files: -[cli_opts.json] -indent_style = tab - -# Set properties for TypeScript files: -[*.ts] -indent_style = tab - -# Set properties for Python files: -[*.{py,py.txt}] -indent_style = space -indent_size = 4 - -# Set properties for Julia files: -[*.{jl,jl.txt}] -indent_style = tab - -# Set properties for R files: -[*.{R,R.txt}] -indent_style = tab - -# Set properties for C files: -[*.{c,c.txt}] -indent_style = tab - -# Set properties for C header files: -[*.{h,h.txt}] -indent_style = tab - -# Set properties for C++ files: -[*.{cpp,cpp.txt}] -indent_style = tab - -# Set properties for C++ header files: -[*.{hpp,hpp.txt}] -indent_style = tab - -# Set properties for Fortran files: -[*.{f,f.txt}] -indent_style = space -indent_size = 2 - -# Set properties for shell files: -[*.{sh,sh.txt}] -indent_style = tab - -# Set properties for AWK files: -[*.{awk,awk.txt}] -indent_style = tab - -# Set properties for HTML files: -[*.{html,html.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for XML files: -[*.{xml,xml.txt}] -indent_style = tab -tab_width = 2 - -# Set properties for CSS files: -[*.{css,css.txt}] -indent_style = tab - -# Set properties for Makefiles: -[Makefile] -indent_style = tab - -[*.{mk,mk.txt}] -indent_style = tab - -# Set properties for Markdown files: -[*.{md,md.txt}] -indent_style = space -indent_size = 4 -trim_trailing_whitespace = true # Note: this disables using two spaces to force a hard line break, which is permitted in Markdown. As we don't typically follow that practice (TMK), we should be safe to automatically trim. - -# Set properties for `usage.txt` files: -[usage.txt] -indent_style = space -indent_size = 2 - -# Set properties for `repl.txt` files: -[repl.txt] -indent_style = space -indent_size = 4 - -# Set properties for `package.json` files: -[package.{json,json.txt}] -indent_style = space -indent_size = 2 - -# Set properties for `datapackage.json` files: -[datapackage.json] -indent_style = space -indent_size = 2 - -# Set properties for `manifest.json` files: -[manifest.json] -indent_style = space -indent_size = 2 - -# Set properties for `tsconfig.json` files: -[tsconfig.json] -indent_style = space -indent_size = 2 - -# Set properties for LaTeX files: -[*.{tex,tex.txt}] -indent_style = tab - -# Set properties for LaTeX Bibliography files: -[*.{bib,bib.txt}] -indent_style = tab - -# Set properties for YAML files: -[*.{yml,yml.txt}] -indent_style = space -indent_size = 2 - -# Set properties for GYP files: -[binding.gyp] -indent_style = space -indent_size = 2 - -[*.gypi] -indent_style = space -indent_size = 2 - -# Set properties for citation files: -[*.{cff,cff.txt}] -indent_style = space -indent_size = 2 diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 5f30286..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1 +0,0 @@ -/* For the `eslint` rules of this project, consult the main repository at https://github.com/stdlib-js/stdlib */ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1c88e69..0000000 --- a/.gitattributes +++ /dev/null @@ -1,66 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2017 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Configuration file which assigns attributes to pathnames. -# -# [1]: https://git-scm.com/docs/gitattributes - -# Automatically normalize the line endings of any committed text files: -* text=auto - -# Override line endings for certain files on checkout: -*.crlf.csv text eol=crlf - -# Denote that certain files are binary and should not be modified: -*.png binary -*.jpg binary -*.jpeg binary -*.gif binary -*.ico binary -*.gz binary -*.zip binary -*.7z binary -*.mp3 binary -*.mp4 binary -*.mov binary - -# Override what is considered "vendored" by GitHub's linguist: -/lib/node_modules/** -linguist-vendored -linguist-generated - -# Configure directories which should *not* be included in GitHub language statistics: -/deps/** linguist-vendored -/dist/** linguist-generated -/workshops/** linguist-vendored - -benchmark/** linguist-vendored -docs/* linguist-documentation -etc/** linguist-vendored -examples/** linguist-documentation -scripts/** linguist-vendored -test/** linguist-vendored -tools/** linguist-vendored - -# Configure files which should *not* be included in GitHub language statistics: -Makefile linguist-vendored -*.mk linguist-vendored -*.jl linguist-vendored -*.py linguist-vendored -*.R linguist-vendored - -# Configure files which should be included in GitHub language statistics: -docs/types/*.d.ts -linguist-documentation diff --git a/.github/.keepalive b/.github/.keepalive deleted file mode 100644 index 64d6ac0..0000000 --- a/.github/.keepalive +++ /dev/null @@ -1 +0,0 @@ -2025-03-17T01:05:34.998Z diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 0cef08f..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,7 +0,0 @@ - - -We are excited about your pull request, but unfortunately we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. - -If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. You may also consult the [development guide](https://github.com/stdlib-js/stdlib/blob/develop/docs/contributing/development.md) for help on developing stdlib. - -We look forward to receiving your contribution! :smiley: \ No newline at end of file diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index e4f10fe..0000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: benchmark - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run benchmarks: - benchmark: - - # Define a display name: - name: 'Run benchmarks' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run benchmarks: - - name: 'Run benchmarks' - run: | - npm run benchmark diff --git a/.github/workflows/cancel.yml b/.github/workflows/cancel.yml deleted file mode 100644 index b5291db..0000000 --- a/.github/workflows/cancel.yml +++ /dev/null @@ -1,57 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: cancel - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to cancel existing workflow runs: - cancel: - - # Define a display name: - name: 'Cancel workflow runs' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Time limit: - timeout-minutes: 3 - - # Define the sequence of job steps... - steps: - - # Cancel existing workflow runs: - - name: 'Cancel existing workflow runs' - # Pin action to full length commit SHA - uses: styfle/cancel-workflow-action@85880fa0301c86cca9da44039ee3bb12d3bedbfa # v0.12.1 - with: - workflow_id: >- - benchmark.yml, - examples.yml, - test.yml, - test_coverage.yml, - test_install.yml, - publish.yml - access_token: ${{ github.token }} diff --git a/.github/workflows/close_pull_requests.yml b/.github/workflows/close_pull_requests.yml deleted file mode 100644 index 0c72c62..0000000 --- a/.github/workflows/close_pull_requests.yml +++ /dev/null @@ -1,54 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: close_pull_requests - -# Workflow triggers: -on: - pull_request_target: - types: [opened] - -# Workflow jobs: -jobs: - - # Define job to close all pull requests: - run: - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Close pull request - - name: 'Close pull request' - # Pin action to full length commit SHA corresponding to v3.1.2 - uses: superbrothers/close-pull-request@9c18513d320d7b2c7185fb93396d0c664d5d8448 - with: - comment: | - Thank you for submitting a pull request. :raised_hands: - - We greatly appreciate your willingness to submit a contribution. However, we are not accepting pull requests against this repository, as all development happens on the [main project repository](https://github.com/stdlib-js/stdlib). - - We kindly request that you submit this pull request against the [respective directory](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce) of the main repository where we’ll review and provide feedback. If this is your first stdlib contribution, be sure to read the [contributing guide](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md) which provides guidelines and instructions for submitting contributions. - - Thank you again, and we look forward to receiving your contribution! :smiley: - - Best, - The stdlib team \ No newline at end of file diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml deleted file mode 100644 index 2984901..0000000 --- a/.github/workflows/examples.yml +++ /dev/null @@ -1,64 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2021 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: examples - -# Workflow triggers: -on: - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job to run the package examples... - examples: - - # Define display name: - name: 'Run examples' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - - # Checkout repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Run examples: - - name: 'Run examples' - run: | - npm run examples diff --git a/.github/workflows/npm_downloads.yml b/.github/workflows/npm_downloads.yml deleted file mode 100644 index cc520b0..0000000 --- a/.github/workflows/npm_downloads.yml +++ /dev/null @@ -1,112 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: npm_downloads - -# Workflow triggers: -on: - # Run this workflow weekly: - schedule: - # cron: ' ' - - cron: '44 8 * * 3' - - # Allow the workflow to be manually run: - workflow_dispatch: - -# Workflow jobs: -jobs: - - # Define a job for retrieving npm download counts... - npm_downloads: - - # Define display name: - name: 'Retrieve npm download counts' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - timeout-minutes: 10 - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Resolve package name: - - name: 'Resolve package name' - id: package_name - run: | - name=`node -e 'console.log(require("./package.json").name)' | tr -d '\n'` - echo "package_name=$name" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Fetch download data: - - name: 'Fetch data' - id: download_data - run: | - url="https://api.npmjs.org/downloads/range/$(date --date='1 year ago' '+%Y-%m-%d'):$(date '+%Y-%m-%d')/${{ steps.package_name.outputs.package_name }}" - echo "$url" - data=$(curl "$url") - mkdir ./tmp - echo "$data" > ./tmp/npm_downloads.json - echo "data=$data" >> $GITHUB_OUTPUT - timeout-minutes: 5 - - # Print summary of download data: - - name: 'Print summary' - run: | - echo "| Date | Downloads |" >> $GITHUB_STEP_SUMMARY - echo "|------|------------|" >> $GITHUB_STEP_SUMMARY - cat ./tmp/npm_downloads.json | jq -r ".downloads | .[-14:] | to_entries | map(\"| \(.value.day) | \(.value.downloads) |\") |.[]" >> $GITHUB_STEP_SUMMARY - - # Upload the download data: - - name: 'Upload data' - # Pin action to full length commit SHA - uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1 - with: - # Define a name for the uploaded artifact (ensuring a unique name for each job): - name: npm_downloads - - # Specify the path to the file to upload: - path: ./tmp/npm_downloads.json - - # Specify the number of days to retain the artifact (default is 90 days): - retention-days: 90 - timeout-minutes: 10 - if: success() - - # Send data to events server: - - name: 'Post data' - # Pin action to full length commit SHA - uses: distributhor/workflow-webhook@48a40b380ce4593b6a6676528cd005986ae56629 # v3.0.3 - env: - webhook_url: ${{ secrets.STDLIB_NPM_DOWNLOADS_URL }} - webhook_secret: ${{ secrets.STDLIB_WEBHOOK_SECRET }} - data: '{ "downloads": ${{ steps.download_data.outputs.data }} }' - timeout-minutes: 5 - if: success() diff --git a/.github/workflows/productionize.yml b/.github/workflows/productionize.yml deleted file mode 100644 index f4575e9..0000000 --- a/.github/workflows/productionize.yml +++ /dev/null @@ -1,794 +0,0 @@ -#/ -# @license Apache-2.0 -# -# Copyright (c) 2022 The Stdlib Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#/ - -# Workflow name: -name: productionize - -# Workflow triggers: -on: - # Run workflow when a new commit is pushed to the main branch: - push: - branches: - - main - - # Allow the workflow to be manually run: - workflow_dispatch: - inputs: - require-passing-tests: - description: 'Require passing tests for creating bundles' - type: boolean - default: true - - # Run workflow upon completion of `publish` workflow run: - workflow_run: - workflows: ["publish"] - types: [completed] - - -# Concurrency group to prevent multiple concurrent executions: -concurrency: - group: productionize - cancel-in-progress: true - -# Workflow jobs: -jobs: - - # Define a job to create a production build... - productionize: - - # Define display name: - name: 'Productionize' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Define the sequence of job steps... - steps: - # Checkout main branch of repository: - - name: 'Checkout main branch' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - ref: main - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Create production branch: - - name: 'Create production branch' - run: | - git checkout -b production - - # Transform error messages: - - name: 'Transform error messages' - id: transform-error-messages - uses: stdlib-js/transform-errors-action@main - - # Change `@stdlib/string-format` to `@stdlib/error-tools-fmtprodmsg` in package.json if the former is a dependency, otherwise insert it as a dependency: - - name: 'Update dependencies in package.json' - run: | - PKG_VERSION=$(npm view @stdlib/error-tools-fmtprodmsg version) - if grep -q '"@stdlib/string-format"' package.json; then - sed -i "s/\"@stdlib\/string-format\": \"^.*\"/\"@stdlib\/error-tools-fmtprodmsg\": \"^$PKG_VERSION\"/g" package.json - else - node -e "var pkg = require( './package.json' ); pkg.dependencies[ '@stdlib/error-tools-fmtprodmsg' ] = '^$PKG_VERSION'; require( 'fs' ).writeFileSync( 'package.json', JSON.stringify( pkg, null, 2 ) );" - fi - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Transform error messages" - - # Push changes: - - name: 'Push changes' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" production --force - - # Define a job for running tests of the productionized code... - test: - - # Define a display name: - name: 'Run Tests' - - # Define the type of virtual host machine: - runs-on: 'ubuntu-latest' - - # Indicate that this job depends on the prior job finishing: - needs: productionize - - # Run this job regardless of the outcome of the prior job: - if: always() - - # Define the sequence of job steps... - steps: - - # Checkout the repository: - - name: 'Checkout repository' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - with: - # Use the `production` branch: - ref: production - - # Install Node.js: - - name: 'Install Node.js' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Build native add-on if present: - - name: 'Build native add-on (if present)' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - run: | - if [ -f "binding.gyp" ]; then - npm install node-gyp --no-save && ./node_modules/.bin/node-gyp rebuild - fi - - # Run tests: - - name: 'Run tests' - if: ${{ github.event.inputs.require-passing-tests == 'true' }} - id: tests - run: | - npm test || npm test || npm test - - # Define job to create a bundle for use in Deno... - deno: - - # Define display name: - name: 'Create Deno bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `deno` branch exists: - - name: 'Check if remote `deno` branch exists' - id: deno-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin deno - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `deno` exists, delete everything in branch and merge `production` into it - - name: 'If `deno` exists, delete everything in branch and merge `production` into it' - if: steps.deno-branch-exists.outputs.remote-exists - run: | - git checkout -b deno origin/deno - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `deno` does not exist, create `deno` branch: - - name: 'If `deno` does not exist, create `deno` branch' - if: ${{ steps.deno-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b deno - - # Copy files to deno directory: - - name: 'Copy files to deno directory' - run: | - mkdir -p deno - cp README.md LICENSE CONTRIBUTORS NOTICE ./deno - - # Copy TypeScript definitions to deno directory: - if [ -d index.d.ts ]; then - cp index.d.ts ./deno/index.d.ts - fi - if [ -e ./docs/types/index.d.ts ]; then - cp ./docs/types/index.d.ts ./deno/mod.d.ts - fi - - # Install Node.js: - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: Install production and development dependencies - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Bundle package for use in Deno: - - name: 'Bundle package for Deno' - id: deno-bundle - uses: stdlib-js/bundle-action@main - with: - target: 'deno' - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - # Replace links to other packages with links to the deno branch: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/deno/"; - - # Replace reference to `@stdlib/types` with CDN link: - find ./deno -type f -name '*.ts' -print0 | xargs -0 -r sed -Ei "s/\/\/\/ /\/\/\/ /g" - - # Change wording of project description to avoid reference to JavaScript and Node.js: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "s/a standard library for JavaScript and Node.js, /a standard library /g" - - # Rewrite all `require()`s to use jsDelivr links: - find ./deno -type f -name '*.md' -print0 | xargs -0 sed -Ei "/require\( '@stdlib\// { - s/(var|let|const)\s+([a-z0-9_]+)\s+=\s*require\( '([^']+)' \);/import \2 from \'\3\';/i - s/@stdlib/https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js/ - s/';/@deno\/mod.js';/ - }" - - # Rewrite first `import` to show importing of named exports if available: - exports=$(cat lib/index.js | \ - grep -E 'setReadOnly\(.*,.*,.*\)' | \ - sed -E 's/setReadOnly\((.*),(.*),(.*)\);/\2/' | \ - sed -E "s/'//g" | \ - sort) - if [ -n "$exports" ]; then - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\nimport\s+([a-zA-Z0-9_]+)\s+from\s*'([^']+)';\n\`\`\`/\`\`\`javascript\nimport \1 from '\2';\n\`\`\`\n\nYou can also import the following named exports from the package:\n\n\`\`\`javascript\nimport { $(echo $exports | sed -E 's/ /, /g') } from '\2';\n\`\`\`/" - fi - - # Remove `installation`, `cli`, and `c` sections: - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./deno -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Create package.json file for deno branch: - jq --indent 2 '{"name": .name, "version": .version, "description": .description, "license": .license, "type": "module", "main": "./mod.js", "homepage": .homepage, "repository": .repository, "bugs": .bugs, "keywords": .keywords, "funding": .funding}' package.json > ./deno/package.json - - # Delete everything in current directory aside from deno folder: - - name: 'Delete everything in current directory aside from deno folder' - run: | - find . -type 'f' | grep -v -e "deno" -e ".git/" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e "deno" -e ".git" | xargs -r rm -rf - - # Move deno directory to root: - - name: 'Move deno directory to root' - run: | - mv ./deno/* . - rmdir ./deno - - # Commit changes: - - name: 'Commit changes' - run: | - git add -A - git commit -m "Auto-generated commit" - - # Push changes to `deno` branch: - - name: 'Push changes to `deno` branch' - run: | - SLUG=${{ github.repository }} - echo "Pushing changes to $SLUG..." - git push "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$SLUG.git" deno - - # Send status to Slack channel if job fails: - - name: 'Send status to Slack channel in case of failure' - # Pin action to full length commit SHA - uses: 8398a7/action-slack@28ba43ae48961b90635b50953d216767a6bea486 # v3.16.2 - with: - status: ${{ job.status }} - channel: '#npm-ci' - if: failure() - - # Define job to create a UMD bundle... - umd: - - # Define display name: - name: 'Create UMD bundle' - - # Define the type of virtual host machine on which to run the job: - runs-on: ubuntu-latest - - # Indicate that this job depends on the test job finishing: - needs: test - - # Define the sequence of job steps... - steps: - # Checkout the repository: - - name: 'Checkout repository' - # Pin action to full length commit SHA - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 - - # Configure Git: - - name: 'Configure Git' - run: | - git config --local user.email "noreply@stdlib.io" - git config --local user.name "stdlib-bot" - - # Check if remote `umd` branch exists: - - name: 'Check if remote `umd` branch exists' - id: umd-branch-exists - continue-on-error: true - run: | - git fetch --all - git ls-remote --exit-code --heads origin umd - if [ $? -eq 0 ]; then - echo "remote-exists=true" >> $GITHUB_OUTPUT - else - echo "remote-exists=false" >> $GITHUB_OUTPUT - fi - - # If `umd` exists, delete everything in branch and merge `production` into it - - name: 'If `umd` exists, delete everything in branch and merge `production` into it' - if: steps.umd-branch-exists.outputs.remote-exists - run: | - git checkout -b umd origin/umd - - find . -type 'f' | grep -v -e ".git/" -e "package.json" -e "README.md" -e "LICENSE" -e "CONTRIBUTORS" -e "NOTICE" | xargs -r rm - find . -mindepth 1 -type 'd' | grep -v -e ".git" | xargs -r rm -rf - - git add -A - git commit -m "Remove files" --allow-empty - - git config merge.theirs.name 'simulate `-s theirs`' - git config merge.theirs.driver 'cat %B > %A' - GIT_CONFIG_PARAMETERS="'merge.default=theirs'" git merge origin/production --allow-unrelated-histories - - # Copy files from `production` branch if necessary: - git checkout origin/production -- . - if [ -n "$(git status --porcelain)" ]; then - git add -A - git commit -m "Auto-generated commit" - fi - - # If `umd` does not exist, create `umd` branch: - - name: 'If `umd` does not exist, create `umd` branch' - if: ${{ steps.umd-branch-exists.outputs.remote-exists == false }} - run: | - git checkout production - git checkout -b umd - - # Copy files to umd directory: - - name: 'Copy files to umd directory' - run: | - mkdir -p umd - cp README.md LICENSE CONTRIBUTORS NOTICE ./umd - - # Install Node.js - - name: 'Install Node.js' - # Pin action to full length commit SHA - uses: actions/setup-node@b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 # v4.0.1 - with: - node-version: 20 - timeout-minutes: 5 - - # Install dependencies: - - name: 'Install production and development dependencies' - id: install - run: | - npm install || npm install || npm install - timeout-minutes: 15 - - # Extract alias: - - name: 'Extract alias' - id: extract-alias - run: | - alias=$(grep -E 'require\(' README.md | head -n 1 | sed -E 's/^var ([a-zA-Z0-9_]+) = .+/\1/') - echo "alias=${alias}" >> $GITHUB_OUTPUT - - # Create Universal Module Definition (UMD) Node.js bundle: - - name: 'Create Universal Module Definition (UMD) Node.js bundle' - id: umd-bundle-node - uses: stdlib-js/bundle-action@main - with: - target: 'umd-node' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Create Universal Module Definition (UMD) browser bundle: - - name: 'Create Universal Module Definition (UMD) browser bundle' - id: umd-bundle-browser - uses: stdlib-js/bundle-action@main - with: - target: 'umd-browser' - alias: ${{ steps.extract-alias.outputs.alias }} - - # Rewrite file contents: - - name: 'Rewrite file contents' - run: | - - # Replace links to other packages with links to the umd branch: - find ./umd -type f -name '*.md' -print0 | xargs -0 sed -Ei "/\/tree\/main/b; /^\[@stdlib[^:]+: https:\/\/github.com\/stdlib-js\// s/(.*)/\\1\/tree\/umd/"; - - # Remove `installation`, `cli`, and `c` sections: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/
[^<]+<\/section>//g;" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.cli \-\->//g" - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?
[\s\S]+<\!\-\- \/.c \-\->//g" - - # Rewrite first `require()` to show consumption of the UMD bundle in Observable and via a `script` tag: - find ./umd -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/\`\`\`javascript\n(var|let|const)\s+([a-zA-Z0-9_]+)\s+=\s*require\( '\@stdlib\/([^']+)' \);\n\`\`\`/To use in Observable,\n\n\`\`\`javascript\n\2 = require\( 'https:\/\/cdn.jsdelivr.net\/gh\/stdlib-js\/\3\@umd\/browser.js' \)\n\`\`\`\n\nTo vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:\n\n\`\`\`javascript\nvar \2 = require\( 'path\/to\/vendor\/umd\/\3\/index.js' \)\n\`\`\`\n\nTo include the bundle in a webpage,\n\n\`\`\`html\n + + ```
@@ -460,7 +457,7 @@ reduceAsync( files, acc, read, done ); ## Notice -This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. +This package is part of [stdlib][stdlib], a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. @@ -531,11 +528,11 @@ Copyright © 2016-2025. The Stdlib [Authors][stdlib-authors]. -[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each +[@stdlib/utils/async/for-each]: https://github.com/stdlib-js/utils-async-for-each/tree/esm -[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce +[@stdlib/utils/reduce]: https://github.com/stdlib-js/utils-reduce/tree/esm -[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right +[@stdlib/utils/async/reduce-right]: https://github.com/stdlib-js/utils-async-reduce-right/tree/esm diff --git a/SECURITY.md b/SECURITY.md deleted file mode 100644 index 9702d4c..0000000 --- a/SECURITY.md +++ /dev/null @@ -1,5 +0,0 @@ -# Security - -> Policy for reporting security vulnerabilities. - -See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security). diff --git a/benchmark/benchmark.factory.js b/benchmark/benchmark.factory.js deleted file mode 100644 index b68285b..0000000 --- a/benchmark/benchmark.factory.js +++ /dev/null @@ -1,73 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var factory = require( './../lib/factory.js' ); - - -// MAIN // - -bench( pkg+':factory', function benchmark( b ) { - var reduceAsync; - var arr; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - reduceAsync = factory( onItem ); - - i = 0; - b.tic(); - - return next(); - - function next( error ) { - var acc; - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js deleted file mode 100644 index a47001c..0000000 --- a/benchmark/benchmark.js +++ /dev/null @@ -1,160 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench-harness' ); -var EPS = require( '@stdlib/constants-float64-eps' ); -var pkg = require( './../package.json' ).name; -var reduceAsync = require( './../lib' ); - - -// MAIN // - -bench( pkg, function benchmark( b ) { - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':series=false', function benchmark( b ) { - var opts; - var arr; - var acc; - var len; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'series': false - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); - -bench( pkg+':limit=3', function benchmark( b ) { - var opts; - var arr; - var len; - var acc; - var i; - - function onItem( acc, v, i, clbk ) { - setTimeout( onTimeout, 0 ); - function onTimeout() { - acc.sum += v; - clbk( null, acc ); - } - } - opts = { - 'limit': 3 - }; - arr = new Array( 100 ); - len = arr.length; - for ( i = 0; i < len; i++ ) { - arr[ i ] = EPS; - } - i = 0; - b.tic(); - - return next(); - - function next( error ) { - i += 1; - if ( error ) { - b.fail( 'should not return an error' ); - } - if ( i <= b.iterations ) { - arr[ 0 ] += 10.0; - acc = { - 'sum': 0.0 - }; - return reduceAsync( arr, acc, opts, onItem, next ); - } - b.toc(); - b.pass( 'benchmark finished' ); - b.end(); - } -}); diff --git a/branches.md b/branches.md deleted file mode 100644 index 426dade..0000000 --- a/branches.md +++ /dev/null @@ -1,56 +0,0 @@ - - -# Branches - -This repository has the following branches: - -- **main**: default branch generated from the [stdlib project][stdlib-url], where all development takes place. -- **production**: [production build][production-url] of the package (e.g., reformatted error messages to reduce bundle sizes and thus the number of bytes transmitted over a network). -- **esm**: [ES Module][esm-url] branch for use via a `script` tag without the need for installation and bundlers (see [README][esm-readme]). -- **deno**: [Deno][deno-url] branch for use in Deno (see [README][deno-readme]). -- **umd**: [UMD][umd-url] branch for use in Observable, or in dual browser/Node.js environments (see [README][umd-readme]). - -The following diagram illustrates the relationships among the above branches: - -```mermaid -graph TD; -A[stdlib]-->|generate standalone package|B; -B[main] -->|productionize| C[production]; -C -->|bundle| D[esm]; -C -->|bundle| E[deno]; -C -->|bundle| F[umd]; - -%% click A href "https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce" -%% click B href "https://github.com/stdlib-js/utils-async-reduce/tree/main" -%% click C href "https://github.com/stdlib-js/utils-async-reduce/tree/production" -%% click D href "https://github.com/stdlib-js/utils-async-reduce/tree/esm" -%% click E href "https://github.com/stdlib-js/utils-async-reduce/tree/deno" -%% click F href "https://github.com/stdlib-js/utils-async-reduce/tree/umd" -``` - -[stdlib-url]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/async/reduce -[production-url]: https://github.com/stdlib-js/utils-async-reduce/tree/production -[deno-url]: https://github.com/stdlib-js/utils-async-reduce/tree/deno -[deno-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/deno/README.md -[umd-url]: https://github.com/stdlib-js/utils-async-reduce/tree/umd -[umd-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/umd/README.md -[esm-url]: https://github.com/stdlib-js/utils-async-reduce/tree/esm -[esm-readme]: https://github.com/stdlib-js/utils-async-reduce/blob/esm/README.md \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 4e5a7a8..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -import reduceAsync from '../docs/types/index'; -export = reduceAsync; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index cea069b..0000000 --- a/dist/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict";var c=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var E=c(function(W,O){ -var j=require('@stdlib/assert-is-plain-object/dist'),d=require('@stdlib/assert-has-own-property/dist'),k=require('@stdlib/assert-is-boolean/dist').isPrimitive,J=require('@stdlib/assert-is-positive-integer/dist').isPrimitive,h=require('@stdlib/error-tools-fmtprodmsg/dist');function L(e,r){return j(r)?(d(r,"thisArg")&&(e.thisArg=r.thisArg),d(r,"series")&&(e.series=r.series,!k(e.series))?new TypeError(h('1Rb2o',"series",e.series)):d(r,"limit")&&(e.limit=r.limit,!J(e.limit))?new TypeError(h('1Rb3P',"limit",e.limit)):null):new TypeError(h('1Rb2V',r));}O.exports=L -});var T=c(function(X,P){ -var S=require("debug"),l=S("reduce-async:limit");function B(e,r,i,n,u){var v,m,o,a,s,t,g;if(s=e.length,l("Collection length: %d",s),s===0)return l("Finished processing a collection."),u(null,r);for(s1){if(n=D(i,e),n)throw n;u=r}else u=e;if(!F(u))throw new TypeError(y('1Rb3q',u));return i.series===void 0&&i.limit===void 0&&(i.series=!0),i.series?i.limit=1:i.limit||(i.limit=z),v;function v(m,o,a){if(!R(m))throw new TypeError(y('1RbAh',m));if(!F(a))throw new TypeError(y('1Rb3q',a));return G(m,o,i,u,s);function s(t,g){if(t)return a(t);a(null,g)}}}p.exports=H -});var V=c(function(Z,N){ -var C=q();function K(e,r,i,n,u){if(arguments.length<5)return C(i)(e,r,n);C(i,n)(e,r,u)}N.exports=K -});var M=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),I=V(),Q=q();M(I,"factory",Q);module.exports=I; -/** @license Apache-2.0 */ -//# sourceMappingURL=index.js.map diff --git a/dist/index.js.map b/dist/index.js.map deleted file mode 100644 index 63055fa..0000000 --- a/dist/index.js.map +++ /dev/null @@ -1,7 +0,0 @@ -{ - "version": 3, - "sources": ["../lib/validate.js", "../lib/limit.js", "../lib/factory.js", "../lib/main.js", "../lib/index.js"], - "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a positive integer. Option: `%s`.', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar logger = require( 'debug' );\n\n\n// VARIABLES //\n\nvar debug = logger( 'reduce-async:limit' );\n\n\n// MAIN //\n\n/**\n* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions.\n*\n* @private\n* @param {Collection} collection - input collection\n* @param {*} acc - initial value\n* @param {Options} opts - function options\n* @param {*} [opts.thisArg] - execution context\n* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations\n* @param {Function} fcn - function to invoke\n* @param {Callback} done - function to invoke upon completion or upon encountering an error\n* @returns {void}\n*/\nfunction limit( collection, acc, opts, fcn, done ) {\n\tvar maxIndex;\n\tvar count;\n\tvar flg;\n\tvar lim;\n\tvar len;\n\tvar idx;\n\tvar i;\n\n\tlen = collection.length;\n\tdebug( 'Collection length: %d', len );\n\n\tif ( len === 0 ) {\n\t\tdebug( 'Finished processing a collection.' );\n\t\treturn done( null, acc );\n\t}\n\tif ( len < opts.limit ) {\n\t\tlim = len;\n\t} else {\n\t\tlim = opts.limit;\n\t}\n\tdebug( 'Concurrency limit: %d', lim );\n\tdebug( 'Number of arguments: %d', fcn.length );\n\n\tmaxIndex = len - 1;\n\tcount = 0;\n\tidx = -1;\n\tfor ( i = 0; i < lim; i++ ) {\n\t\t// This guard is necessary to protect against synchronous functions which exhaust all collection elements...\n\t\tif ( idx < maxIndex ) {\n\t\t\tnext(); // eslint-disable-line node/callback-return\n\t\t}\n\t}\n\t/**\n\t* Callback to invoke a provided function for the next element in a collection.\n\t*\n\t* @private\n\t*/\n\tfunction next() {\n\t\tidx += 1;\n\t\tdebug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) );\n\t\tif ( fcn.length === 3 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], cb );\n\t\t} else if ( fcn.length === 4 ) {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, cb );\n\t\t} else {\n\t\t\tfcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len\n\t\t}\n\t\t/**\n\t\t* Callback invoked once a provided function finishes processing a collection element.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [result] - accumulation result\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction cb( error, result ) {\n\t\t\tif ( flg ) {\n\t\t\t\t// Prevent further processing of collection elements:\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif ( error ) {\n\t\t\t\tflg = true;\n\t\t\t\treturn clbk( error );\n\t\t\t}\n\t\t\tdebug( 'Accumulator: %s', JSON.stringify( result ) );\n\t\t\tacc = result;\n\t\t\tclbk();\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked once ready to process the next collection element.\n\t*\n\t* @private\n\t* @param {*} [error] - error\n\t* @returns {void}\n\t*/\n\tfunction clbk( error ) {\n\t\tif ( error ) {\n\t\t\tdebug( 'Encountered an error: %s', error.message );\n\t\t\treturn done( error );\n\t\t}\n\t\tcount += 1;\n\t\tdebug( 'Processed %d of %d collection elements.', count, len );\n\t\tif ( idx < maxIndex ) {\n\t\t\treturn next();\n\t\t}\n\t\tif ( count === len ) {\n\t\t\tdebug( 'Finished processing a collection.' );\n\t\t\treturn done( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = limit;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar format = require( '@stdlib/string-format' );\nvar PINF = require( '@stdlib/constants-float64-pinf' );\nvar validate = require( './validate.js' );\nvar limit = require( './limit.js' );\n\n\n// MAIN //\n\n/**\n* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} last argument must be a function\n* @returns {Function} function which invokes the provided function once for each element in a collection\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error, data ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var opts = {\n* 'series': false\n* };\n*\n* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently:\n* var reduceAsync = factory( opts, read );\n*\n* // Create a collection over which to iterate:\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* // Define a callback which handles errors:\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* // Run `read` for each element in `files`:\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, done );\n*/\nfunction factory( options, fcn ) {\n\tvar opts;\n\tvar err;\n\tvar f;\n\n\topts = {};\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = factory;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nmodule.exports = reduceAsync;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* var readFile = require( '@stdlib/fs-read-file' );\n* var reduceAsync = require( '@stdlib/utils-async-reduce' );\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar factory = require( './factory.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"], - "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAW,QAAS,gCAAiC,EACrDC,EAAa,QAAS,iCAAkC,EACxDC,EAAY,QAAS,2BAA4B,EAAE,YACnDC,EAAoB,QAAS,oCAAqC,EAAE,YACpEC,EAAS,QAAS,uBAAwB,EA4B9C,SAASC,EAAUC,EAAMC,EAAU,CAClC,OAAMP,EAAUO,CAAQ,GAGnBN,EAAYM,EAAS,SAAU,IACnCD,EAAK,QAAUC,EAAQ,SAEnBN,EAAYM,EAAS,QAAS,IAClCD,EAAK,OAASC,EAAQ,OACjB,CAACL,EAAWI,EAAK,MAAO,GACrB,IAAI,UAAWF,EAAQ,+DAAgE,SAAUE,EAAK,MAAO,CAAE,EAGnHL,EAAYM,EAAS,OAAQ,IACjCD,EAAK,MAAQC,EAAQ,MAChB,CAACJ,EAAmBG,EAAK,KAAM,GAC5B,IAAI,UAAWF,EAAQ,wEAAyE,QAASE,EAAK,KAAM,CAAE,EAGxH,MAjBC,IAAI,UAAWF,EAAQ,qEAAsEG,CAAQ,CAAE,CAkBhH,CAKAR,EAAO,QAAUM,IC/EjB,IAAAG,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,QAAS,OAAQ,EAK1BC,EAAQD,EAAQ,oBAAqB,EAkBzC,SAASE,EAAOC,EAAYC,EAAKC,EAAMC,EAAKC,EAAO,CAClD,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAW,OACjBF,EAAO,wBAAyBW,CAAI,EAE/BA,IAAQ,EACZ,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,EAaxB,IAXKQ,EAAMP,EAAK,MACfM,EAAMC,EAEND,EAAMN,EAAK,MAEZJ,EAAO,wBAAyBU,CAAI,EACpCV,EAAO,0BAA2BK,EAAI,MAAO,EAE7CE,EAAWI,EAAM,EACjBH,EAAQ,EACRI,EAAM,GACAC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,EAAK,EAQP,SAASA,GAAO,CACfF,GAAO,EACPZ,EAAO,6BAA8BY,EAAK,KAAK,UAAWV,EAAYU,CAAI,CAAE,CAAE,EACzEP,EAAI,SAAW,EACnBA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGG,CAAG,EACxCV,EAAI,SAAW,EAC1BA,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKG,CAAG,EAExDV,EAAI,KAAMD,EAAK,QAASD,EAAKD,EAAYU,CAAI,EAAGA,EAAKV,EAAYa,CAAG,EAUrE,SAASA,EAAIC,EAAOC,EAAS,CAC5B,GAAK,CAAAR,EAIL,IAAKO,EACJ,OAAAP,EAAM,GACCS,EAAMF,CAAM,EAEpBhB,EAAO,kBAAmB,KAAK,UAAWiB,CAAO,CAAE,EACnDd,EAAMc,EACNC,EAAK,EACN,CACD,CASA,SAASA,EAAMF,EAAQ,CACtB,GAAKA,EACJ,OAAAhB,EAAO,2BAA4BgB,EAAM,OAAQ,EAC1CV,EAAMU,CAAM,EAIpB,GAFAR,GAAS,EACTR,EAAO,0CAA2CQ,EAAOG,CAAI,EACxDC,EAAML,EACV,OAAOO,EAAK,EAEb,GAAKN,IAAUG,EACd,OAAAX,EAAO,mCAAoC,EACpCM,EAAM,KAAMH,CAAI,CAEzB,CACD,CAKAL,EAAO,QAAUG,IC/IjB,IAAAkB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAe,QAAS,8BAA+B,EACvDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,gCAAiC,EACjDC,EAAW,IACXC,EAAQ,IAoEZ,SAASC,EAASC,EAASC,EAAM,CAChC,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAC,EACH,UAAU,OAAS,EAAI,CAE3B,GADAC,EAAMN,EAAUK,EAAMF,CAAQ,EACzBG,EACJ,MAAMA,EAEPC,EAAIH,CACL,MACCG,EAAIJ,EAEL,GAAK,CAACP,EAAYW,CAAE,EACnB,MAAM,IAAI,UAAWT,EAAQ,mEAAoES,CAAE,CAAE,EAEtG,OAAKF,EAAK,SAAW,QAAUA,EAAK,QAAU,SAC7CA,EAAK,OAAS,IAEVA,EAAK,OACTA,EAAK,MAAQ,EACDA,EAAK,QACjBA,EAAK,MAAQN,GAEPS,EAaP,SAASA,EAAaC,EAAYC,EAASC,EAAO,CACjD,GAAK,CAACd,EAAcY,CAAW,EAC9B,MAAM,IAAI,UAAWX,EAAQ,sEAAuEW,CAAW,CAAE,EAElH,GAAK,CAACb,EAAYe,CAAK,EACtB,MAAM,IAAI,UAAWb,EAAQ,mEAAoEa,CAAK,CAAE,EAEzG,OAAOV,EAAOQ,EAAYC,EAASL,EAAME,EAAGK,CAAK,EAUjD,SAASA,EAAMC,EAAOC,EAAM,CAC3B,GAAKD,EACJ,OAAOF,EAAME,CAAM,EAEpBF,EAAM,KAAMG,CAAI,CACjB,CACD,CACD,CAKAnB,EAAO,QAAUO,ICnKjB,IAAAa,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IA8Dd,SAASC,EAAaC,EAAYC,EAASC,EAASC,EAAKC,EAAO,CAC/D,GAAK,UAAU,OAAS,EACvB,OAAON,EAASI,CAAQ,EAAGF,EAAYC,EAASE,CAAI,EAErDL,EAASI,EAASC,CAAI,EAAGH,EAAYC,EAASG,CAAK,CACpD,CAKAP,EAAO,QAAUE,IC9BjB,IAAIM,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD", - "names": ["require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "isPositiveInteger", "format", "validate", "opts", "options", "require_limit", "__commonJSMin", "exports", "module", "logger", "debug", "limit", "collection", "acc", "opts", "fcn", "done", "maxIndex", "count", "flg", "lim", "len", "idx", "i", "next", "cb", "error", "result", "clbk", "require_factory", "__commonJSMin", "exports", "module", "isFunction", "isCollection", "format", "PINF", "validate", "limit", "factory", "options", "fcn", "opts", "err", "f", "reduceAsync", "collection", "initial", "done", "clbk", "error", "acc", "require_main", "__commonJSMin", "exports", "module", "factory", "reduceAsync", "collection", "initial", "options", "fcn", "done", "setReadOnly", "main", "factory"] -} diff --git a/docs/repl.txt b/docs/repl.txt deleted file mode 100644 index 3d38a61..0000000 --- a/docs/repl.txt +++ /dev/null @@ -1,223 +0,0 @@ - -{{alias}}( collection, initial, [options,] reducer, done ) - Applies a function against an accumulator and each element in a collection - and returns the accumulated result. - - When invoked, `reducer` is provided a maximum of five arguments: - - - accumulator: accumulated value. - - value: collection value. - - index: collection index. - - collection: the input collection. - - next: a callback to be invoked after processing a collection `value`. - - The actual number of provided arguments depends on function length. If - `reducer` accepts three arguments, `reducer` is provided: - - - accumulator - - value - - next - - If `reducer` accepts four arguments, `reducer` is provided: - - - accumulator - - value - - index - - next - - For every other `reducer` signature, `reducer` is provided all five - arguments. - - The `next` callback accepts two arguments: - - - error: error argument. - - accumulator: accumulated value. - - If a provided function calls the `next` callback with a truthy `error` - argument, the function suspends execution and immediately calls the `done` - callback for subsequent `error` handling. - - Execution is *not* guaranteed to be asynchronous. To guarantee asynchrony, - wrap the `done` callback in a function which either executes at the end of - the current stack (e.g., `nextTick`) or during a subsequent turn of the - event loop (e.g., `setImmediate`, `setTimeout`). - - The function does not support dynamic collection resizing. - - If provided an empty collection, the function invokes the `done` callback - with the `initial` value as the second argument. - - The function does not skip `undefined` elements. - - When processing collection elements concurrently, *beware* of race - conditions when updating an accumulator. This is especially true when an - accumulator is a primitive (e.g., a number). In general, prefer object - accumulators. - - Parameters - ---------- - collection: Array|TypedArray|Object - Input collection over which to iterate. If provided an object, the - object must be array-like (excluding strings and functions). - - initial: any - Accumulator value used in the first invocation of the reduction - function. - - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - reducer: Function - The function to invoke for each element in a collection. - - done: Function - A callback invoked either upon processing all collection elements or - upon encountering an error. - - Examples - -------- - // Basic usage: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, fcn, done ) - 3000 - 2500 - 1000 - 6500 - - // Limit number of concurrent invocations: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'limit': 2 }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 2500 - 3000 - 1000 - 6500 - - // Process concurrently: - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var opts = { 'series': false }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > {{alias}}( arr, acc, opts, fcn, done ) - 1000 - 2500 - 3000 - 6500 - - -{{alias}}.factory( [options,] fcn ) - Returns a function which applies a function against an accumulator and each - element in a collection and returns the accumulated result. - - Parameters - ---------- - options: Object (optional) - Function options. - - options.limit: integer (optional) - Maximum number of pending invocations. Default: Infinity. - - options.series: boolean (optional) - Boolean indicating whether to process each collection element - sequentially. Default: true. - - options.thisArg: any (optional) - Execution context. - - fcn: Function - The function to invoke for each element in a collection. - - Returns - ------- - out: Function - A function which invokes a function for each element in a collection. - - Examples - -------- - > function fcn( acc, value, index, next ) { - ... setTimeout( onTimeout, value ); - ... function onTimeout() { - ... console.log( value ); - ... acc.sum += value; - ... next( null, acc ); - ... } - ... }; - > var opts = { 'series': false }; - > var f = {{alias}}.factory( opts, fcn ); - > function done( error, acc ) { - ... if ( error ) { - ... throw error; - ... } - ... console.log( acc.sum ); - ... }; - > var arr = [ 3000, 2500, 1000 ]; - > var acc = { 'sum': 0 }; - > f( arr, acc, done ) - 1000 - 2500 - 3000 - 6500 - > acc = { 'sum': 0 }; - > arr = [ 2000, 1500, 1000 ]; - > f( arr, acc, done ) - 1000 - 1500 - 2000 - 4500 - - See Also - -------- - diff --git a/docs/types/test.ts b/docs/types/test.ts deleted file mode 100644 index dc65786..0000000 --- a/docs/types/test.ts +++ /dev/null @@ -1,164 +0,0 @@ -/* -* @license Apache-2.0 -* -* Copyright (c) 2021 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -import reduceAsync = require( './index' ); - -const reducer = ( acc: any, value: number, index: number, next: Function ) => { - acc.sum += value; - if ( index === void 0 ) { - throw new Error( '`index` is missing.' ); - } - next( null, acc ); -}; - -const done = ( error: Error | null, acc: any ) => { - if ( error ) { - throw error; - } - if ( acc === void 0 ) { - throw new Error( '`acc` is missing.' ); - } -}; - - -// TESTS // - -// The function returns void... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, done ); // $ExpectType void -} - -// The compiler throws an error if the function is provided a first argument which is not a collection... -{ - const acc = { 'sum': 0 }; - reduceAsync( 2, acc, reducer, done ); // $ExpectError - reduceAsync( false, acc, reducer, done ); // $ExpectError - reduceAsync( true, acc, reducer, done ); // $ExpectError - reduceAsync( {}, acc, reducer, done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a predicate argument which is not a predicate function... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, 2, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, false, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, true, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, 'abc', done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, done ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, [], done ); // $ExpectError -} - -// The compiler throws an error if the function is provided a done callback argument which is not a function having a supported signature... -{ - const acc = { 'sum': 0 }; - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 2 ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, false ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, true ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, 'abc' ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, {} ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, [] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function is provided an invalid number of arguments... -{ - const acc = { 'sum': 0 }; - reduceAsync(); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ] ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, reducer ); // $ExpectError - reduceAsync( [ 3000, 2500, 1000 ], acc, {}, reducer, done, {} ); // $ExpectError -} - -// Attached to main export is a `factory` method which returns a function... -{ - reduceAsync.factory( reducer ); // $ExpectType FactoryFunction - reduceAsync.factory( { 'series': true }, reducer ); // $ExpectType FactoryFunction -} - -// The compiler throws an error if the `factory` method is provided an options argument which is not an object... -{ - reduceAsync.factory( [], reducer ); // $ExpectError - reduceAsync.factory( 123, reducer ); // $ExpectError - reduceAsync.factory( 'abc', reducer ); // $ExpectError - reduceAsync.factory( false, reducer ); // $ExpectError - reduceAsync.factory( true, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a last argument which is not a predicate function... -{ - reduceAsync.factory( {} ); // $ExpectError - reduceAsync.factory( true ); // $ExpectError - reduceAsync.factory( false ); // $ExpectError - reduceAsync.factory( {}, 123 ); // $ExpectError - reduceAsync.factory( {}, 'abc' ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided invalid arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1( 12, acc, done ); // $ExpectError - fcn1( true, acc, done ); // $ExpectError - fcn1( false, acc, done ); // $ExpectError - fcn1( {}, acc, done ); // $ExpectError - - fcn1( [ 3000, 2500, 1000 ], acc, 12 ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, true ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, false ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, '5' ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, {} ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, [] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, ( x: number ): number => x ); // $ExpectError -} - -// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments... -{ - const acc = { 'sum': 0 }; - const fcn1 = reduceAsync.factory( reducer ); - fcn1(); // $ExpectError - fcn1( [ 3000, 2500, 1000 ] ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc ); // $ExpectError - fcn1( [ 3000, 2500, 1000 ], acc, done, {} ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `limit` option which is not a number... -{ - reduceAsync.factory( { 'limit': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': true }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': false }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'limit': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided a `series` option which is not a boolean... -{ - reduceAsync.factory( { 'series': '12' }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': 12 }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': {} }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': [] }, reducer ); // $ExpectError - reduceAsync.factory( { 'series': ( x: number ): number => x }, reducer ); // $ExpectError -} - -// The compiler throws an error if the `factory` method is provided an invalid number of arguments... -{ - reduceAsync.factory(); // $ExpectError - reduceAsync.factory( {}, reducer, {} ); // $ExpectError -} diff --git a/examples/index.js b/examples/index.js deleted file mode 100644 index 4ef0cc0..0000000 --- a/examples/index.js +++ /dev/null @@ -1,55 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -var resolve = require( 'path' ).resolve; -var readFile = require( '@stdlib/fs-read-file' ); -var reduceAsync = require( './../lib' ); - -var files = [ - resolve( __dirname, '..', 'package.json' ), - resolve( __dirname, '..', 'README.md' ) -]; - -function done( error, acc ) { - if ( error ) { - throw error; - } - console.log( acc.count ); -} - -function read( acc, file, next ) { - var opts = { - 'encoding': 'utf8' - }; - readFile( file, opts, onFile ); - - function onFile( error ) { - if ( error ) { - return next( null, acc ); - } - acc.count += 1; - next( null, acc ); - } -} - -var acc = { - 'count': 0 -}; -reduceAsync( files, acc, read, done ); diff --git a/docs/types/index.d.ts b/index.d.ts similarity index 99% rename from docs/types/index.d.ts rename to index.d.ts index 7022881..b296f97 100644 --- a/docs/types/index.d.ts +++ b/index.d.ts @@ -18,7 +18,7 @@ // TypeScript Version: 4.1 -/// +/// import { Collection } from '@stdlib/types/array'; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..3ce8090 --- /dev/null +++ b/index.mjs @@ -0,0 +1,4 @@ +// Copyright (c) 2025 The Stdlib Authors. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 +/// +import e from"https://cdn.jsdelivr.net/gh/stdlib-js/utils-define-nonenumerable-read-only-property@v0.2.1-esm/index.mjs";import t from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-function@v0.2.2-esm/index.mjs";import i from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-collection@v0.2.2-esm/index.mjs";import r from"https://cdn.jsdelivr.net/gh/stdlib-js/error-tools-fmtprodmsg@v0.2.2-esm/index.mjs";import s from"https://cdn.jsdelivr.net/gh/stdlib-js/constants-float64-pinf@v0.2.2-esm/index.mjs";import n from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-plain-object@v0.2.2-esm/index.mjs";import o from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-has-own-property@v0.2.2-esm/index.mjs";import{isPrimitive as l}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-boolean@v0.2.2-esm/index.mjs";import{isPrimitive as c}from"https://cdn.jsdelivr.net/gh/stdlib-js/assert-is-positive-integer@v0.2.2-esm/index.mjs";function m(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var i=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};i.prototype=t.prototype}else i={};return Object.defineProperty(i,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(i,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})})),i}var d=m(Object.freeze({__proto__:null,default:()=>()=>{}}))("reduce-async:limit");function f(e,m){var f,u,p;if(f={},arguments.length>1){if(u=function(e,t){return n(t)?(o(t,"thisArg")&&(e.thisArg=t.thisArg),o(t,"series")&&(e.series=t.series,!l(e.series))?new TypeError(r("1Rb2o","series",e.series)):o(t,"limit")&&(e.limit=t.limit,!c(e.limit))?new TypeError(r("1Rb3P","limit",e.limit)):null):new TypeError(r("1Rb2V",t))}(f,e),u)throw u;p=m}else p=e;if(!t(p))throw new TypeError(r("1Rb3q",p));return void 0===f.series&&void 0===f.limit&&(f.series=!0),f.series?f.limit=1:f.limit||(f.limit=s),function(e,s,n){if(!i(e))throw new TypeError(r("1RbAh",e));if(!t(n))throw new TypeError(r("1Rb3q",n));return function(e,t,i,r,s){var n,o,l,c,m,f,u;if(m=e.length,d("Collection length: %d",m),0===m)return d("Finished processing a collection."),s(null,t);for(c=m 1 ) {\n\t\terr = validate( opts, options );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t\tf = fcn;\n\t} else {\n\t\tf = options;\n\t}\n\tif ( !isFunction( f ) ) {\n\t\tthrow new TypeError( format( '1Rb3q', f ) );\n\t}\n\tif ( opts.series === void 0 && opts.limit === void 0 ) {\n\t\topts.series = true;\n\t}\n\tif ( opts.series ) {\n\t\topts.limit = 1;\n\t} else if ( !opts.limit ) {\n\t\topts.limit = PINF;\n\t}\n\treturn reduceAsync;\n\n\t/**\n\t* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n\t*\n\t* @private\n\t* @param {Collection} collection - input collection\n\t* @param {*} initial - initial value\n\t* @param {Callback} done - function to invoke upon completion\n\t* @throws {TypeError} first argument must be a collection\n\t* @throws {TypeError} last argument must be a function\n\t* @returns {void}\n\t*/\n\tfunction reduceAsync( collection, initial, done ) {\n\t\tif ( !isCollection( collection ) ) {\n\t\t\tthrow new TypeError( format( '1RbAh', collection ) );\n\t\t}\n\t\tif ( !isFunction( done ) ) {\n\t\t\tthrow new TypeError( format( '1Rb3q', done ) );\n\t\t}\n\t\treturn limit( collection, initial, opts, f, clbk );\n\n\t\t/**\n\t\t* Callback invoked upon completion.\n\t\t*\n\t\t* @private\n\t\t* @param {*} [error] - error\n\t\t* @param {*} [acc] - accumulated value\n\t\t* @returns {void}\n\t\t*/\n\t\tfunction clbk( error, acc ) {\n\t\t\tif ( error ) {\n\t\t\t\treturn done( error );\n\t\t\t}\n\t\t\tdone( null, acc );\n\t\t}\n\t}\n}\n\n\n// EXPORTS //\n\nexport default factory;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport isObject from '@stdlib/assert-is-plain-object';\nimport hasOwnProp from '@stdlib/assert-has-own-property';\nimport { isPrimitive as isBoolean } from '@stdlib/assert-is-boolean';\nimport { isPrimitive as isPositiveInteger } from '@stdlib/assert-is-positive-integer';\nimport format from '@stdlib/error-tools-fmtprodmsg';\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'thisArg': {},\n* 'series': false,\n* 'limit': 10\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( '1Rb2V', options ) );\n\t}\n\tif ( hasOwnProp( options, 'thisArg' ) ) {\n\t\topts.thisArg = options.thisArg;\n\t}\n\tif ( hasOwnProp( options, 'series' ) ) {\n\t\topts.series = options.series;\n\t\tif ( !isBoolean( opts.series ) ) {\n\t\t\treturn new TypeError( format( '1Rb2o', 'series', opts.series ) );\n\t\t}\n\t}\n\tif ( hasOwnProp( options, 'limit' ) ) {\n\t\topts.limit = options.limit;\n\t\tif ( !isPositiveInteger( opts.limit ) ) {\n\t\t\treturn new TypeError( format( '1Rb3P', 'limit', opts.limit ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nexport default validate;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nimport factory from './factory.js';\n\n\n// MAIN //\n\n/**\n* Applies a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* ## Notes\n*\n* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.\n* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).\n*\n* @param {Collection} collection - input collection\n* @param {*} initial - initial value\n* @param {Options} [options] - function options\n* @param {*} [options.thisArg] - execution context\n* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time\n* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection\n* @param {Function} fcn - function to invoke for each element in a collection\n* @param {Callback} done - function to invoke upon completion\n* @throws {TypeError} first argument must be a collection\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @throws {TypeError} second-to-last argument must be a function\n* @throws {TypeError} last argument must be a function\n* @returns {void}\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\nfunction reduceAsync( collection, initial, options, fcn, done ) {\n\tif ( arguments.length < 5 ) {\n\t\treturn factory( options )( collection, initial, fcn );\n\t}\n\tfactory( options, fcn )( collection, initial, done );\n}\n\n\n// EXPORTS //\n\nexport default reduceAsync;\n","/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Apply a function against an accumulator and each element in a collection and return the accumulated result.\n*\n* @module @stdlib/utils-async-reduce\n*\n* @example\n* import readFile from '@stdlib/fs-read-file';\n* import reduceAsync from '@stdlib/utils-async-reduce';\n*\n* var files = [\n* './beep.js',\n* './boop.js'\n* ];\n*\n* function done( error, acc ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( acc.count );\n* }\n*\n* function read( acc, file, next ) {\n* var opts = {\n* 'encoding': 'utf8'\n* };\n* readFile( file, opts, onFile );\n*\n* function onFile( error ) {\n* if ( error ) {\n* return next( null, acc );\n* }\n* acc.count += 1;\n* next( null, acc );\n* }\n* }\n*\n* var acc = {\n* 'count': 0\n* };\n* reduceAsync( files, acc, read, done );\n*/\n\n// MODULES //\n\nimport setReadOnly from '@stdlib/utils-define-nonenumerable-read-only-property';\nimport main from './main.js';\nimport factory from './factory.js';\n\n\n// MAIN //\n\nsetReadOnly( main, 'factory', factory );\n\n\n// EXPORTS //\n\nexport default main;\n"],"names":["debug","logger","factory","options","fcn","opts","err","f","arguments","length","isObject","hasOwnProp","thisArg","series","isBoolean","TypeError","format","limit","isPositiveInteger","validate","isFunction","PINF","collection","initial","done","isCollection","acc","maxIndex","count","flg","lim","len","idx","i","next","cb","error","result","clbk","JSON","stringify","call","message","reduceAsync","setReadOnly","main"],"mappings":";;81CA2BIA,uDAAQC,CAAQ,sBCoEpB,SAASC,EAASC,EAASC,GAC1B,IAAIC,EACAC,EACAC,EAGJ,GADAF,EAAO,CAAA,EACFG,UAAUC,OAAS,EAAI,CAE3B,GADAH,EChDF,SAAmBD,EAAMF,GACxB,OAAMO,EAAUP,IAGXQ,EAAYR,EAAS,aACzBE,EAAKO,QAAUT,EAAQS,SAEnBD,EAAYR,EAAS,YACzBE,EAAKQ,OAASV,EAAQU,QAChBC,EAAWT,EAAKQ,SACd,IAAIE,UAAWC,EAAQ,QAAS,SAAUX,EAAKQ,SAGnDF,EAAYR,EAAS,WACzBE,EAAKY,MAAQd,EAAQc,OACfC,EAAmBb,EAAKY,QACtB,IAAIF,UAAWC,EAAQ,QAAS,QAASX,EAAKY,QAGhD,MAjBC,IAAIF,UAAWC,EAAQ,QAASb,GAkBzC,CD4BQgB,CAAUd,EAAMF,GACjBG,EACJ,MAAMA,EAEPC,EAAIH,CACN,MACEG,EAAIJ,EAEL,IAAMiB,EAAYb,GACjB,MAAM,IAAIQ,UAAWC,EAAQ,QAAST,IAUvC,YARqB,IAAhBF,EAAKQ,aAAoC,IAAfR,EAAKY,QACnCZ,EAAKQ,QAAS,GAEVR,EAAKQ,OACTR,EAAKY,MAAQ,EACDZ,EAAKY,QACjBZ,EAAKY,MAAQI,GAed,SAAsBC,EAAYC,EAASC,GAC1C,IAAMC,EAAcH,GACnB,MAAM,IAAIP,UAAWC,EAAQ,QAASM,IAEvC,IAAMF,EAAYI,GACjB,MAAM,IAAIT,UAAWC,EAAQ,QAASQ,IAEvC,ODhGF,SAAgBF,EAAYI,EAAKrB,EAAMD,EAAKoB,GAC3C,IAAIG,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAKJ,GAHAF,EAAMT,EAAWb,OACjBT,EAAO,wBAAyB+B,GAEnB,IAARA,EAEJ,OADA/B,EAAO,qCACAwB,EAAM,KAAME,GAapB,IAVCI,EADIC,EAAM1B,EAAKY,MACTc,EAEA1B,EAAKY,MAEZjB,EAAO,wBAAyB8B,GAChC9B,EAAO,0BAA2BI,EAAIK,QAEtCkB,EAAWI,EAAM,EACjBH,EAAQ,EACRI,GAAO,EACDC,EAAI,EAAGA,EAAIH,EAAKG,IAEhBD,EAAML,GACVO,IAQF,SAASA,IAkBR,SAASC,EAAIC,EAAOC,GACnB,IAAKR,EAAL,CAIA,GAAKO,EAEJ,OADAP,GAAM,EACCS,EAAMF,GAEdpC,EAAO,kBAAmBuC,KAAKC,UAAWH,IAC1CX,EAAMW,EACNC,GAPC,CAQD,CA5BDtC,EAAO,6BADPgC,GAAO,EACmCO,KAAKC,UAAWlB,EAAYU,KAClD,IAAf5B,EAAIK,OACRL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOG,GACtB,IAAf/B,EAAIK,OACfL,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKG,GAErD/B,EAAIqC,KAAMpC,EAAKO,QAASc,EAAKJ,EAAYU,GAAOA,EAAKV,EAAYa,EAuBlE,CASD,SAASG,EAAMF,GACd,OAAKA,GACJpC,EAAO,2BAA4BoC,EAAMM,SAClClB,EAAMY,KAGdpC,EAAO,0CADP4B,GAAS,EACgDG,GACpDC,EAAML,EACHO,IAEHN,IAAUG,GACd/B,EAAO,qCACAwB,EAAM,KAAME,SAFpB,EAIA,CACF,CCGST,CAAOK,EAAYC,EAASlB,EAAME,GAUzC,SAAe6B,EAAOV,GACrB,GAAKU,EACJ,OAAOZ,EAAMY,GAEdZ,EAAM,KAAME,EACZ,GACD,CACF,CE1EA,SAASiB,EAAarB,EAAYC,EAASpB,EAASC,EAAKoB,GACxD,GAAKhB,UAAUC,OAAS,EACvB,OAAOP,EAASC,EAATD,CAAoBoB,EAAYC,EAASnB,GAEjDF,EAASC,EAASC,EAAlBF,CAAyBoB,EAAYC,EAASC,EAC/C,CClBAoB,EAAAC,EAAA,UAAA3C"} \ No newline at end of file diff --git a/lib/factory.js b/lib/factory.js deleted file mode 100644 index 1b5acc5..0000000 --- a/lib/factory.js +++ /dev/null @@ -1,164 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isFunction = require( '@stdlib/assert-is-function' ); -var isCollection = require( '@stdlib/assert-is-collection' ); -var format = require( '@stdlib/error-tools-fmtprodmsg' ); -var PINF = require( '@stdlib/constants-float64-pinf' ); -var validate = require( './validate.js' ); -var limit = require( './limit.js' ); - - -// MAIN // - -/** -* Returns a function to apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} last argument must be a function -* @returns {Function} function which invokes the provided function once for each element in a collection -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error, data ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var opts = { -* 'series': false -* }; -* -* // Create a `reduceAsync` function which invokes `read` for each collection element concurrently: -* var reduceAsync = factory( opts, read ); -* -* // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* // Define a callback which handles errors: -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* // Run `read` for each element in `files`: -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, done ); -*/ -function factory( options, fcn ) { - var opts; - var err; - var f; - - opts = {}; - if ( arguments.length > 1 ) { - err = validate( opts, options ); - if ( err ) { - throw err; - } - f = fcn; - } else { - f = options; - } - if ( !isFunction( f ) ) { - throw new TypeError( format( '1Rb3q', f ) ); - } - if ( opts.series === void 0 && opts.limit === void 0 ) { - opts.series = true; - } - if ( opts.series ) { - opts.limit = 1; - } else if ( !opts.limit ) { - opts.limit = PINF; - } - return reduceAsync; - - /** - * Applies a function against an accumulator and each element in a collection and return the accumulated result. - * - * @private - * @param {Collection} collection - input collection - * @param {*} initial - initial value - * @param {Callback} done - function to invoke upon completion - * @throws {TypeError} first argument must be a collection - * @throws {TypeError} last argument must be a function - * @returns {void} - */ - function reduceAsync( collection, initial, done ) { - if ( !isCollection( collection ) ) { - throw new TypeError( format( '1RbAh', collection ) ); - } - if ( !isFunction( done ) ) { - throw new TypeError( format( '1Rb3q', done ) ); - } - return limit( collection, initial, opts, f, clbk ); - - /** - * Callback invoked upon completion. - * - * @private - * @param {*} [error] - error - * @param {*} [acc] - accumulated value - * @returns {void} - */ - function clbk( error, acc ) { - if ( error ) { - return done( error ); - } - done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = factory; diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 3883a4a..0000000 --- a/lib/index.js +++ /dev/null @@ -1,77 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -/** -* Apply a function against an accumulator and each element in a collection and return the accumulated result. -* -* @module @stdlib/utils-async-reduce -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* var reduceAsync = require( '@stdlib/utils-async-reduce' ); -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ - -// MODULES // - -var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' ); -var main = require( './main.js' ); -var factory = require( './factory.js' ); - - -// MAIN // - -setReadOnly( main, 'factory', factory ); - - -// EXPORTS // - -module.exports = main; diff --git a/lib/limit.js b/lib/limit.js deleted file mode 100644 index a98ce4c..0000000 --- a/lib/limit.js +++ /dev/null @@ -1,144 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var logger = require( 'debug' ); - - -// VARIABLES // - -var debug = logger( 'reduce-async:limit' ); - - -// MAIN // - -/** -* Invokes a function once for each element in a collection, limiting the number of concurrently pending functions. -* -* @private -* @param {Collection} collection - input collection -* @param {*} acc - initial value -* @param {Options} opts - function options -* @param {*} [opts.thisArg] - execution context -* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations -* @param {Function} fcn - function to invoke -* @param {Callback} done - function to invoke upon completion or upon encountering an error -* @returns {void} -*/ -function limit( collection, acc, opts, fcn, done ) { - var maxIndex; - var count; - var flg; - var lim; - var len; - var idx; - var i; - - len = collection.length; - debug( 'Collection length: %d', len ); - - if ( len === 0 ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - if ( len < opts.limit ) { - lim = len; - } else { - lim = opts.limit; - } - debug( 'Concurrency limit: %d', lim ); - debug( 'Number of arguments: %d', fcn.length ); - - maxIndex = len - 1; - count = 0; - idx = -1; - for ( i = 0; i < lim; i++ ) { - // This guard is necessary to protect against synchronous functions which exhaust all collection elements... - if ( idx < maxIndex ) { - next(); // eslint-disable-line node/callback-return - } - } - /** - * Callback to invoke a provided function for the next element in a collection. - * - * @private - */ - function next() { - idx += 1; - debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); - if ( fcn.length === 3 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], cb ); - } else if ( fcn.length === 4 ) { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); - } else { - fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len - } - /** - * Callback invoked once a provided function finishes processing a collection element. - * - * @private - * @param {*} [error] - error - * @param {*} [result] - accumulation result - * @returns {void} - */ - function cb( error, result ) { - if ( flg ) { - // Prevent further processing of collection elements: - return; - } - if ( error ) { - flg = true; - return clbk( error ); - } - debug( 'Accumulator: %s', JSON.stringify( result ) ); - acc = result; - clbk(); - } - } - - /** - * Callback invoked once ready to process the next collection element. - * - * @private - * @param {*} [error] - error - * @returns {void} - */ - function clbk( error ) { - if ( error ) { - debug( 'Encountered an error: %s', error.message ); - return done( error ); - } - count += 1; - debug( 'Processed %d of %d collection elements.', count, len ); - if ( idx < maxIndex ) { - return next(); - } - if ( count === len ) { - debug( 'Finished processing a collection.' ); - return done( null, acc ); - } - } -} - - -// EXPORTS // - -module.exports = limit; diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ebe6069..0000000 --- a/lib/main.js +++ /dev/null @@ -1,95 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var factory = require( './factory.js' ); - - -// MAIN // - -/** -* Applies a function against an accumulator and each element in a collection and return the accumulated result. -* -* ## Notes -* -* - If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling. -* - This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`). -* -* @param {Collection} collection - input collection -* @param {*} initial - initial value -* @param {Options} [options] - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series=true] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @param {Function} fcn - function to invoke for each element in a collection -* @param {Callback} done - function to invoke upon completion -* @throws {TypeError} first argument must be a collection -* @throws {TypeError} options argument must be an object -* @throws {TypeError} must provide valid options -* @throws {TypeError} second-to-last argument must be a function -* @throws {TypeError} last argument must be a function -* @returns {void} -* -* @example -* var readFile = require( '@stdlib/fs-read-file' ); -* -* function done( error, acc ) { -* if ( error ) { -* throw error; -* } -* console.log( acc.count ); -* } -* -* function read( acc, file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, acc ); -* } -* acc.count += 1; -* next( null, acc ); -* } -* } -* -* var files = [ -* './beep.js', -* './boop.js' -* ]; -* var acc = { -* 'count': 0 -* }; -* reduceAsync( files, acc, read, done ); -*/ -function reduceAsync( collection, initial, options, fcn, done ) { - if ( arguments.length < 5 ) { - return factory( options )( collection, initial, fcn ); - } - factory( options, fcn )( collection, initial, done ); -} - - -// EXPORTS // - -module.exports = reduceAsync; diff --git a/lib/validate.js b/lib/validate.js deleted file mode 100644 index 7f6ee29..0000000 --- a/lib/validate.js +++ /dev/null @@ -1,80 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var isObject = require( '@stdlib/assert-is-plain-object' ); -var hasOwnProp = require( '@stdlib/assert-has-own-property' ); -var isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive; -var isPositiveInteger = require( '@stdlib/assert-is-positive-integer' ).isPrimitive; -var format = require( '@stdlib/error-tools-fmtprodmsg' ); - - -// MAIN // - -/** -* Validates function options. -* -* @private -* @param {Object} opts - destination object -* @param {Options} options - function options -* @param {*} [options.thisArg] - execution context -* @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time -* @param {boolean} [options.series] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function for the next element in a collection -* @returns {(Error|null)} null or an error object -* -* @example -* var opts = {}; -* var options = { -* 'thisArg': {}, -* 'series': false, -* 'limit': 10 -* }; -* var err = validate( opts, options ); -* if ( err ) { -* throw err; -* } -*/ -function validate( opts, options ) { - if ( !isObject( options ) ) { - return new TypeError( format( '1Rb2V', options ) ); - } - if ( hasOwnProp( options, 'thisArg' ) ) { - opts.thisArg = options.thisArg; - } - if ( hasOwnProp( options, 'series' ) ) { - opts.series = options.series; - if ( !isBoolean( opts.series ) ) { - return new TypeError( format( '1Rb2o', 'series', opts.series ) ); - } - } - if ( hasOwnProp( options, 'limit' ) ) { - opts.limit = options.limit; - if ( !isPositiveInteger( opts.limit ) ) { - return new TypeError( format( '1Rb3P', 'limit', opts.limit ) ); - } - } - return null; -} - - -// EXPORTS // - -module.exports = validate; diff --git a/package.json b/package.json index 8b43a45..9ba7fa6 100644 --- a/package.json +++ b/package.json @@ -3,31 +3,8 @@ "version": "0.2.2", "description": "Apply a function against an accumulator and each element in a collection and return the accumulated result.", "license": "Apache-2.0", - "author": { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - }, - "contributors": [ - { - "name": "The Stdlib Authors", - "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" - } - ], - "main": "./lib", - "directories": { - "benchmark": "./benchmark", - "doc": "./docs", - "example": "./examples", - "lib": "./lib", - "test": "./test" - }, - "types": "./docs/types", - "scripts": { - "test": "make test", - "test-cov": "make test-cov", - "examples": "make examples", - "benchmark": "make benchmark" - }, + "type": "module", + "main": "./index.mjs", "homepage": "https://stdlib.io", "repository": { "type": "git", @@ -36,44 +13,6 @@ "bugs": { "url": "https://github.com/stdlib-js/stdlib/issues" }, - "dependencies": { - "@stdlib/assert-has-own-property": "^0.2.2", - "@stdlib/assert-is-boolean": "^0.2.2", - "@stdlib/assert-is-collection": "^0.2.2", - "@stdlib/assert-is-function": "^0.2.2", - "@stdlib/assert-is-plain-object": "^0.2.2", - "@stdlib/assert-is-positive-integer": "^0.2.2", - "@stdlib/constants-float64-pinf": "^0.2.2", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2", - "@stdlib/types": "^0.4.3", - "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2", - "debug": "^2.6.9", - "@stdlib/error-tools-fmtprodmsg": "^0.2.2" - }, - "devDependencies": { - "@stdlib/constants-float64-eps": "^0.2.2", - "@stdlib/fs-read-file": "^0.2.2", - "@stdlib/utils-noop": "^0.2.2", - "tape": "git+https://github.com/kgryte/tape.git#fix/globby", - "istanbul": "^0.4.1", - "tap-min": "git+https://github.com/Planeshifter/tap-min.git", - "@stdlib/bench-harness": "^0.2.2" - }, - "engines": { - "node": ">=0.10.0", - "npm": ">2.7.0" - }, - "os": [ - "aix", - "darwin", - "freebsd", - "linux", - "macos", - "openbsd", - "sunos", - "win32", - "windows" - ], "keywords": [ "stdlib", "stdutils", diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..6d3e789 --- /dev/null +++ b/stats.html @@ -0,0 +1,4842 @@ + + + + + + + + Rollup Visualizer + + + +
+ + + + + diff --git a/test/dist/test.js b/test/dist/test.js deleted file mode 100644 index a8a9c60..0000000 --- a/test/dist/test.js +++ /dev/null @@ -1,33 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2023 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var main = require( './../../dist' ); - - -// TESTS // - -tape( 'main export is defined', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( main !== void 0, true, 'main export is defined' ); - t.end(); -}); diff --git a/test/test.factory.js b/test/test.factory.js deleted file mode 100644 index ab73ffe..0000000 --- a/test/test.factory.js +++ /dev/null @@ -1,1080 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var factory = require( './../lib/factory.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof factory, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( {}, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - factory( value, next ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - factory( opts, next ); - }; - } -}); - -tape( 'the function returns a function (no options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the function returns a function (options)', function test( t ) { - function next( acc, value, clbk ) { - clbk( null, acc ); - } - t.strictEqual( typeof factory( {}, next ), 'function', 'returns a function' ); - t.end(); -}); - -tape( 'the returned function throws an error if not provided a collection (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a collection (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, noop ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (no options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function throws an error if not provided a callback function (options)', function test( t ) { - var reduceAsync; - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - reduceAsync = factory( {}, next ); - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value ); - }; - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'the returned function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (1 argument)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function returns a function which invokes a provided function with five arguments (2 arguments)', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function returns a function which invokes a provided function with five arguments', function test( t ) { - var reduceAsync; - var values; - var arr; - var acc; - var i; - - reduceAsync = factory( fcn ); - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, done ); - - function fcn() { - var value; - var next; - var acc; - var j; - - acc = arguments[ 0 ]; - value = arguments[ 1 ]; - j = arguments[ 2 ]; - next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( j, i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns accumulated result' ); - t.end(); - } -}); - -tape( 'by default, the returned function processes collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the returned function supports processing collection elements sequentially', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements concurrently', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 100, 250, 300 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function for processing collection elements sequentially (limit = 1)', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 300, 250, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports returning a function which limits the maximum number of collection elements which are processed at any one time', function test( t ) { - var reduceAsync; - var expected; - var count; - var opts; - var arr; - - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 250, 100 ]; - expected = [ 250, 300, 100 ]; - count = -1; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - t.strictEqual( value, expected[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var reduceAsync; - var opts; - var arr; - var ctx; - - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (series)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': true - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'limit': 2 - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 300, 100, 250 ]; - count = 0; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the returned function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var reduceAsync; - var count; - var opts; - var arr; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 500, 500, 500 ]; - count = 0; - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var reduceAsync; - var arr; - - reduceAsync = factory( fcn ); - arr = []; - - reduceAsync( arr, 0, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the returned function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var reduceAsync; - var obj; - var arr; - - reduceAsync = factory( fcn ); - - arr = []; - obj = {}; - reduceAsync( arr, obj, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the returned function does not guarantee asynchronous execution', function test( t ) { - var reduceAsync; - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - reduceAsync = factory( opts, fcn ); - - arr = [ 1, 2, 3 ]; - i = 0; - - reduceAsync( arr, {}, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, acc ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the returned function does not skip empty elements', function test( t ) { - var reduceAsync; - var expected; - var arr; - - reduceAsync = factory( fcn ); - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 701ff74..0000000 --- a/test/test.js +++ /dev/null @@ -1,38 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'attached to the main export is a `factory` method', function test( t ) { - t.strictEqual( typeof reduceAsync.factory, 'function', 'has method' ); - t.end(); -}); diff --git a/test/test.main.js b/test/test.main.js deleted file mode 100644 index c0cb641..0000000 --- a/test/test.main.js +++ /dev/null @@ -1,1020 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var noop = require( '@stdlib/utils-noop' ); -var reduceAsync = require( './../lib' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof reduceAsync, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function throws an error if not provided a collection', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - function noop() {}, - /.*/, - new Date() - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( value, 0, next, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (no options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a function to invoke for each collection element (options)', function test( t ) { - var values; - var i; - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, value, noop ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (no options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, next, value ); - }; - } -}); - -tape( 'the function throws an error if not provided a callback function (options)', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - {}, - [] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, {}, next, value ); - }; - } -}); - -tape( 'the function throws an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, 0 ); - } - - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - reduceAsync( [ 1, 2, 3 ], 0, value, next, noop ); - }; - } -}); - -tape( 'the function throws an error if provided an invalid option', function test( t ) { - var values; - var i; - - function next( acc, value, clbk ) { - clbk( null, acc ); - } - - values = [ - '5', - -5, - 3.14, - 0, - NaN, - true, - false, - null, - void 0, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - var opts = { - 'limit': value - }; - reduceAsync( [ 1, 2, 3 ], 0, opts, next, noop ); - }; - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, next ) { - i += 1; - t.strictEqual( value, values[ i ], 'provides expected value' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function invokes a provided function once for each element in a collection (acc,value,index,collection,next)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, collection, next ) { - i += 1; - t.strictEqual( value, values[ index ], 'provides expected value' ); - t.strictEqual( index, i, 'provides expected index' ); - t.strictEqual( collection, arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (1 argument)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc ) { - var value = arguments[ 1 ]; - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function accepts fewer than 3 arguments, the function invokes a provided function with five arguments (2 arguments)', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value ) { - var next = arguments[ 4 ]; - - i += 1; - - t.strictEqual( value, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, value ); - - function onTimeout() { - acc.sum += value; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'if a provided function length is 0, the function invokes a provided function with five arguments', function test( t ) { - var values; - var arr; - var acc; - var i; - - arr = [ 1, 2, 3 ]; - values = [ 1, 2, 3 ]; - i = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn() { - var next; - var acc; - var v; - - next = arguments[ 4 ]; - acc = arguments[ 0 ]; - v = arguments[ 1 ]; - - i += 1; - - t.strictEqual( v, values[ i ], 'provides expected value' ); - t.strictEqual( arguments[ 2 ], i, 'provides expected index' ); - t.strictEqual( arguments[ 3 ], arr, 'provides input collection' ); - - setTimeout( onTimeout, v ); - - function onTimeout() { - acc.sum += v; - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 6, 'returns expected result' ); - t.end(); - } -}); - -tape( 'by default, the function processes collection elements sequentially', function test( t ) { - var values; - var count; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - reduceAsync( arr, acc, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements concurrently', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 100, 250, 300 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': true - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports processing collection elements sequentially (limit = 1)', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 300, 250, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 1 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports limiting the maximum number of collection elements which are processed at any one time', function test( t ) { - var values; - var count; - var opts; - var arr; - var acc; - - arr = [ 300, 250, 100 ]; - values = [ 250, 300, 100 ]; - count = -1; - - acc = { - 'sum': 0 - }; - opts = { - 'series': false, - 'limit': 2 - }; - reduceAsync( arr, acc, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - acc.sum += value; - t.strictEqual( value, values[ count ], 'provides expected value' ); - next( null, acc ); - } - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc.sum, 650, 'returns expected result' ); - t.end(); - } -}); - -tape( 'the function supports specifying an execution context for the invoked function', function test( t ) { - var opts; - var arr; - var ctx; - - arr = [ 1, 2, 3 ]; - ctx = { - 'count': 0 - }; - opts = { - 'thisArg': ctx - }; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - /* eslint-disable no-invalid-this */ - this.count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( ctx.count, 3, 'updated provided context' ); - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (series)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 1, 2, 3 ]; - opts = { - 'series': true - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - setTimeout( onTimeout, value ); - - function onTimeout() { - count += 1; - next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 1, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent; limit)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 300, 100, 250 ]; - opts = { - 'limit': 2 - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 2, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - if ( index === 1 ) { - return next( new Error( 'beep' ) ); - } - next( null, acc ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if an error is encountered while processing a collection element, the function suspends execution and immediately returns the error (concurrent)', function test( t ) { - var count; - var opts; - var arr; - - arr = [ 500, 500, 500 ]; - opts = { - 'series': false - }; - count = 0; - reduceAsync( arr, {}, opts, fcn, done ); - - function fcn( acc, value, index, next ) { - count += 1; - setTimeout( onTimeout, value ); - - function onTimeout() { - return next( new Error( 'beep' ) ); - } - } - - function done( error ) { - t.strictEqual( count, 3, 'suspends execution' ); - if ( error ) { - t.pass( error.message ); - } else { - t.fail( 'did not return an error' ); - } - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result', function test( t ) { - var arr = []; - reduceAsync( arr, 0, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, 0, 'returns initial value' ); - t.end(); - } -}); - -tape( 'if provided an empty collection, the function never invokes a provided function and returns the initial value as the accumulated result (object)', function test( t ) { - var arr = []; - var obj = {}; - - reduceAsync( arr, obj, fcn, done ); - - function fcn() { - t.fail( 'should never be called' ); - } - - function done( error, acc ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( acc, obj, 'returns initial value' ); - t.end(); - } -}); - -tape( 'the function does not guarantee asynchronous execution', function test( t ) { - var opts; - var arr; - var i; - - opts = { - 'series': false - }; - arr = [ 1, 2, 3 ]; - i = 0; - reduceAsync( arr, {}, opts, fcn, done ); - i = 1; - - function fcn( acc, value, next ) { - next( null, value ); - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.strictEqual( i, 0, 'releases the zalgo' ); - t.end(); - } -}); - -tape( 'the function does not skip empty elements', function test( t ) { - var expected; - var arr; - - arr = [ 1, , , 4 ]; // eslint-disable-line no-sparse-arrays - expected = [ 1, void 0, void 0, 4 ]; - - reduceAsync( arr, {}, fcn, done ); - - function fcn( acc, value, index, next ) { - t.strictEqual( value, expected[ index ], 'provides expected value' ); - setTimeout( onTimeout, 0 ); - - function onTimeout() { - return next( null, acc ); - } - } - - function done( error ) { - if ( error ) { - t.fail( error.message ); - } else { - t.pass( 'did not return an error' ); - } - t.end(); - } -}); diff --git a/test/test.validate.js b/test/test.validate.js deleted file mode 100644 index 7660838..0000000 --- a/test/test.validate.js +++ /dev/null @@ -1,181 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var tape = require( 'tape' ); -var validate = require( './../lib/validate.js' ); - - -// TESTS // - -tape( 'main export is a function', function test( t ) { - t.ok( true, __filename ); - t.strictEqual( typeof validate, 'function', 'main export is a function' ); - t.end(); -}); - -tape( 'the function returns an error if provided an `options` argument which is not an object', function test( t ) { - var values; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - err = validate( {}, values[ i ] ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `limit` option which is not a positive integer', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - -5, - 0, - 3.14, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'limit': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns an error if provided a `series` option which is not a boolean', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'series': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err instanceof TypeError, true, 'returns a type error when provided '+values[i] ); - } - t.end(); -}); - -tape( 'the function accepts any value for the `thisArg` option', function test( t ) { - var values; - var opts; - var err; - var i; - - values = [ - '5', - 5, - true, - false, - void 0, - null, - NaN, - [], - {}, - function noop() {} - ]; - - for ( i = 0; i < values.length; i++ ) { - opts = { - 'thisArg': values[ i ] - }; - err = validate( {}, opts ); - t.strictEqual( err, null, 'returns null '+values[i] ); - } - t.end(); -}); - -tape( 'the function returns `null` if all options are valid', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'thisArg': {}, - 'series': false, - 'limit': 10 - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, options, 'sets options' ); - - t.end(); -}); - -tape( 'the function will ignore unrecognized options', function test( t ) { - var options; - var opts; - var err; - - opts = {}; - options = { - 'beep': true, - 'boop': 'bop' - }; - - err = validate( opts, options ); - t.strictEqual( err, null, 'returns null' ); - t.deepEqual( opts, {}, 'ignores unrecognized options' ); - - t.end(); -});